Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

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

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" 
Height="100"
Width="100" 
>
    <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" />
</asp:ListBox>
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"
TargetControlID="txtTitle" 
DropDownControlID="lstTitles" 
/>
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;
}
</script>
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<

Wellplayed.net
This article has been view 123 times.

Advertisement:

Comments

vinod

Avatar

10/6/2008 2:25:16 AM

the same above code is not working with vs.net 2005,I am getting an error "There is no source code available for the current location." , so pls help me to use this code..

vinod



src
Kris

Avatar

10/21/2008 6:57:12 AM

Where are you getting this error? It sounds almost like you're trying to debug something you may not have the source code for... you might see something like that if AJAX isn't setup correctly or at least not setup the same way I did for this code (might have to use something other than ajaxToolkit for the prefix on the dropdownextender).

I wrote this code initially in Visual Studio 2005, so I wouldn't expect it's a problem with the IDE, probably just some issue with how AJAX is setup for you.

By that's just my guess, you didn't give me a lot to go from. Some source or a better description of when you get the error would be helpful."0" sty
satay

Avatar

12/15/2008 12:02:43 PM

value getting empty when page loads? what to do....go.
satay

Avatar

12/15/2008 1:09:16 PM

value getting empty when page loads? what to do....go.
satay

Avatar

12/15/2008 1:10:05 PM

value getting empty when page loads? what to do....go.
satay

Avatar

12/15/2008 1:35:33 PM

value getting empty when page loads? what to do....go.
lr

Avatar

1/14/2009 4:58:22 AM

1) what is the purposed o your DropDownExtender and what do it do in your program?
2) where is ajax? I only see javascript managing an onchange event

go.ph
lr

Avatar

1/14/2009 5:04:47 AM

1) what is the purposed o your DropDownExtender and what do it do in your program?
2) where is ajax? I only see javascript managing an onchange event

go.ph
lr

Avatar

1/14/2009 5:10:37 AM

1) what is the purposed o your DropDownExtender and what do it do in your program?
2) where is ajax? I only see javascript managing an onchange event

go.ph
Gabe

Avatar

2/4/2009 5:31:08 AM

This is a great control. I successfully used it in the main content of my page for an application I am working on. Now, I am trying to use it in a grid view edit template.

I successfully created the template but can't get the selected item in the list to populate back to the text box. Whenever I try to add attributes to the listbox, I get a compile error telling me the object doesn't exist in the current context. Any ideas?"0" sty
Gabe

Avatar

2/5/2009 5:51:26 AM

I figured it out. I wasn't using the proper syntax to access the grdiview's cells where the controls are stashed. I used the onselectedindexchange event of the listbox (with autopostback set to true) instead of the javascript."0" s
Pallavi

Avatar

2/11/2009 11:37:10 PM

hi....how can i populate the dropdown box from a database?
n.r
Kris

Avatar

2/17/2009 6:27:49 AM

You just need to populate the listbox, no different than you would to populate a normal listbox or a gridview or a dropdown box.
YashRaj

Avatar

3/16/2009 1:12:28 AM

Hi ,

how can i populate data from a combo box to a table frame Data Displayer using Ajax. on select of element from combobox it'll display the related data on a table.



Regards,
Yash<if
urko

Avatar

10/25/2009 6:06:30 AM

Im having problems using this inside the detailsview.
After i add javascript onClick function i get following error:
Copiler Error Message: CS0103: The name 'TextBox4' does not exist in the current context

Textbox4 is the name of txtTitle

i
Fariborz

Avatar

11/24/2009 7:50:17 AM

Could you please give me the VB .net version of the following code:

this.Attributes.Add("onclick", "setText(this.options[this.selectedIndex].value);");

Thanks,

Fariborzne
Bilal sabha

Avatar

12/21/2009 5:30:51 AM

can u send to me the code in VB.NETh
ss

Avatar

12/23/2009 11:45:45 PM

his.Attributes.Add("onclick", "setText(this.options[this.selectedIndex].value);"); src=
Mukesh Patel

Avatar

12/23/2009 11:46:42 PM

this.Attributes.Add("onclick", "setText(this.options[this.selectedIndex].value);");

This is same as the above one...
I am using it in my code,n its working finen.ru
Ajay Verma

Avatar

1/12/2010 2:03:12 AM

Hi

Can u please tell me how can i populate ajax dropdown extender in database table
Ajay Verma

Avatar

1/12/2010 2:17:32 AM

Hi

Can u please tell me how can i populate ajax dropdown extender in database table
Prasa

Avatar

2/22/2010 4:20:44 AM

That was helpful... Thanksig
356

Avatar

6/24/2010 6:23:58 AM

4654564536i


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.