SIGN UP MEMBER LOGIN:    
ARTICLE

DropDownList Selection as you type in

Posted by Sushila Patel Articles | WebForms Controls February 09, 2004
The ASP.NET DropDownList server control allows positioning within the list by typing the first character. This method does not require a trip to the server.
Reader Level:

Introduction:

The ASP.NET DropDownList server control allows positioning within the list by typing the first character. This method does not require a trip to the server.

In this article we'll see how to type in the DropDownList. To test the working we'll place a textbox besides the dropdownlist. We'll type as many characters in the dropdownlist that we want to match in the list and check if they appear in the textbox.

The screen would be with following controls

  • DropdownList 
  • HTML TextBox 
  • Button
  • Label

Screenshot



Code for User Interface goes as follows:

<asp:DropDownList id="DropDownList1" onKeyup="TrapKey(this, this.event)" runat="server"></asp:DropDownList>
<
asp:Button id="Button1" runat="server" Text="Show"></asp:Button>
<
input type="text" disabled name="result" value="test" size="20" ID="Text1">
<
br><asp:Label id="Label4" runat="server" Font-Names="Georgia"></asp:Label>
</
asp:Label>

The Javascript that would allow to type in the dropdown as the user types goes below:

<script language="JavaScript" type="text/javascript">
function
clearWord()
{
userWord = "";
document.forms[0].result.value = "";
}
var userWord = "";
function TrapKey(obj, e)
{
thekey = String.fromCharCode(
event.keyCode);
userWord += thekey;
for (var i = 0; i < obj.options.length; i++) {
var txt = obj.options[i].text.toUpperCase();
document.forms[0].result.value = userWord;
if (txt.indexOf(userWord) == 0) {
obj.options[i].selected =
true;
obj.options[i].focus();
break;
}
}
setTimeout("clearWord()", 3000)
}
</script>

In code behind

C#

SqlConnection mycn;
SqlDataAdapter myda;
DataSet ds;
String strConn;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
try
{
strConn="server=localhost;database=northwind;uid=sa;pwd=;";
mycn =
new SqlConnection(strConn);
myda =
new SqlDataAdapter("select * from Employees", mycn);
ds=
new DataSet ();
myda.Fill (ds,"Table");
DropDownList1.DataSource =ds.Tables [0];
DropDownList1.DataTextField =ds.Tables[0].Columns
"FirstName"].ColumnName.ToString() ;
DropDownList1.DataValueField = ds.Tables[0].Columns
"EmployeeId"].ColumnName.ToString();
DropDownList1.DataBind();
}
catch(Exception ex)
{
Response.Write (ex.Message );
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text="You have selected " + DropDownList1.SelectedItem.Text;
}


The final Output

Login to add your contents and source code to this article
share this article :
post comment
 

You have any solution for typed dropdownlist.(typing in dropdown )

Posted by Nitish kumar Apr 25, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor