AJAX DropDownList Dependancy


V4 Ajax Tutorials
Server Intellect Cloud Hosting

AJAX DropDownList Dependancy

This tutorial was created with Microsoft's Visual Studio.NET 2008, but can be recreated in 2005 with the AJAX Extensions, which can be downloaded at this link.

In this tutorial, you will learn how to use AJAX to create a seamless integration between two dependant DropDownList boxes. We will have one displaying a number of US States, and the second will be dependant upon the selection of the first, displaying some Cities of the chosen State.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

First, we need to create our database. We will add two tables - one for States and one for Cities. The States table will just need an id column and a state column; the City table will need an id column, city column and a state column which refers to the state table.
Once we have our tables set up, we can add the DropDownLists to our ASPX page:

<form id="form1" runat="server">
State: <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" />
<br />
City: <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" />
</form>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

For each of these DropDowns, we will need a Data Source. So we'll add two SqlDataSources like so:

<form id="form1" runat="server">
State: <asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSource1"
DataTextField="state" DataValueField="state" AutoPostBack="true" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT state FROM tblStates ORDER BY state"></asp:SqlDataSource>
<br />
City: <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2"
DataTextField="city" DataValueField="city" AutoPostBack="true" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT city FROM tblCities WHERE state=@state ORDER BY city">
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="ddlState" />
</SelectParameters>
</asp:SqlDataSource>
</form>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Note that the DataSources are selecting the data from the database to display in the DropDowns. We also have a where clause in our second SqlDataSource, which refers @state, which is defined in our ControlParameter - referring to the selected item of our first DropDown.

Using the code above, we will have a working web application, but the page will do a full PostBack when we select a State. To change this, we add a ScriptManager and an UpdatePanel:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

State: <asp:DropDownList ID="ddlState" runat="server" DataSourceID="SqlDataSource1"
DataTextField="state" DataValueField="state" AutoPostBack="true" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT state FROM tblStates ORDER BY state"></asp:SqlDataSource>
<br />
City: <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2"
DataTextField="city" DataValueField="city" AutoPostBack="true" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT city FROM tblCities WHERE state=@state ORDER BY city">
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="ddlState" />
</SelectParameters>
</asp:SqlDataSource>

</ContentTemplate>
</asp:UpdatePanel>
<br />
</form>

The ScriptManager tag handles all our Requests and JavaScript - it does all the work for us. The UpdatePanel merely sets a region for the page to reload. Using AJAX in Visual Studio.NET 2008 couldn't be easier.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
Freelance SEO India said:

India’s leading Freelance SEO India services provider with main competency in Search Engine Optimization of websites.

Posted Dec 29, 2010 11:15 AM
Flights to Istanbul said:

Hi, I agree with every statement that you have made in the post and I really appreciate your effort in gathering up the information. Thanks for it.

Posted Mar 8, 2011 7:15 AM
Flights to Istanbul said:

Hi, I found your post really helpful. It helped me all the way in completing my assignment, I am also giving a referance link of your blog in my case study. Thanks for posting such informative content. Keep posting.

Posted Mar 8, 2011 7:17 AM
Flights to Istanbul said:

Hi, I appreciate the information that you have provided in the post. It is worth noting and I really liked the presentation as well. I will surely come back for more of intersting posts.

Posted Mar 8, 2011 7:24 AM
Capodanno roma said:

I really appreciate your professional approach. These are pieces of very useful information that will be of great use for me in future. Thanks for it.

Posted Mar 23, 2011 3:33 AM
www.outlet-louisvuitton.co.uk said:

www.outlet-louisvuitton.co.uk

Posted Aug 6, 2011 3:31 AM
pandora style bracelets said:
Posted Aug 26, 2011 3:29 AM
thomas sabo charm shop said:

good things

vibram fingers, http://www.acsshoes4sale.com thomas sabo necklace, www.newtomasshop.net/.../Necklaces_6_1.h

Posted Aug 27, 2011 2:16 AM
James Dean said:

<a href="http://dishwashernotdraining.info/">dishwasher wont drain</a>

Thank you for nothing you should rename the Projectin : to make a grid view visible with ajax and not do sorting. There are too many things not metioned, As i read the headline i had expect a script that sorts the Grid without postback, not over the standard functionallity....

Posted Aug 28, 2011 2:17 PM
dishwasher wont drain said:

Very interesting! i'm exited :)

Posted Aug 28, 2011 2:19 PM
Womens true religion crops jeans said:

I will surely share this awesome content to my friends, great job!

Posted Sep 3, 2011 1:24 AM
chanelonsale said:

thank you for sharing this post

Posted Sep 27, 2011 2:13 AM
chanelonsale said:
Posted Sep 27, 2011 2:14 AM
chanelonsale said:

=================================

Posted Sep 27, 2011 2:15 AM
International Background said:

Your blog is so popular that people ha made pages on social media sites to discuss about your pages.

Posted Oct 21, 2011 8:29 AM
pandora style beads said:

I am in support of wonderland, this project is seeing very good fame and should be continued.

Posted Oct 27, 2011 1:30 AM
nishith said:

need to

Posted Dec 19, 2011 4:32 AM
Laptop in BD said:

Men always like to have information on his/her subject of interest from the internet.So all the site should be arranged in a nice manner like this so that men can be highly helped by these sites.

Posted Dec 23, 2011 10:22 AM
burberry schal said:

Saw your article is really too shocked! Too great, really admire! Thank you!

Posted Dec 28, 2011 8:06 AM
AccurateChronometer said:

Slimslad you will find that there is opposition to these changes in the north of England, Isle of Wight, the Rhondda and many places besides.

Posted Jan 9, 2012 4:16 AM
AccurateChronometer said:

Slimslad you will find that there is opposition to these changes in the north of England, Isle of Wight, the Rhondda and many places besides.

Posted Jan 9, 2012 4:16 AM
pet franchises said:

I am very interested in your article, I think your articles are so interesting that I need more information, go is berkaya and I will always support you. I say many thanks to you.

Posted Jan 9, 2012 5:22 AM

Leave a Comment