Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact and I will try and recover/find it.

Using AJAX to Create a DropDownList for Populating a TextBox
Date 7/8/2008    Tags, C#    (0)

For an application I had to develop for work, we had a need for an input field that allowed you to either select an item or type in your own item. Before AJAX, you would have to write create your own ComboBox or create something like an "Other" selection and then have a blank TextBox for your new item. Thankfully today we have AJAX and can accomplish this rather easily using the DropDownExtender, a TextBox, and a ListBox. Because speed was an issue, we also wanted to keep everything clientside.

For this example, I am going to assume you already have AJAX setup for your web application. If not, I suggest going to Microsoft's AJAX website and following their instructions. It's a simple setup, expecially if you have Visual Studio 2008, and their walkthrus do a good job teaching you.

Begin by creating your TextBox and your ListBox. You could use most any other control instead of a ListBox (the Microsoft example uses a panel with LinkButtons), but by using a ListBox you have the option to easily bind it to a DataSource.

<asp:TextBox ID="txtTitle" runat="server" />
<asp:ListBox ID="lstTitles" runat="server" 
    <asp:ListItem Text="One" Value="One" />
    <asp:ListItem Text="Two" Value="Two" />
    <asp:ListItem Text="Three" Value="Three" />
    <asp:ListItem Text="Four" Value="Four" />
    <asp:ListItem Text="Five" Value="Five" />
Now create your DropDownExtender and set the TargetControlID to the ID of your TextBox and set the DropDownControlID to the ID of your ListBox.
<ajaxToolkit:DropDownExtender runat="server" ID="DDE"
This will give you a TextBox that has a down arrow that will open up the ListBox. But when you click the ListBox, it just goes away. So we'll add some JavaScript magic to the onclick event in order to populate the value in your TextBox with the value from the ListBox.
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        lstTitles.Attributes.Add("onclick", "setText(this.options[this.selectedIndex].value);");
Now that we've applied the onClick handler, we just need to write the onClick function.
<script type="text/javascript" language="javascript">
function setText(newValue)
    document.getElementById("<%=txtTitle.ClientID %>").value = newValue;
I used txtTitle.ClientID instead of just the ID of the TextBox so that this would work correctly within the confines of a Master Page.

Compile and run<

This article has been view 20703 times.


No comments for this article.

Add Comments

Name *
  Name the animal in the picture below:

Comment *
Insert Cancel
Things To Click

Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4) (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)