ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
AJAX Cascading DropDownList using jQuery in ASP.Net
Author Name: Mudassar Khan Published Date: March 07, 2010
Filed Under :
ASP.Net
 |
JQuery
Views: 4319
Here Mudassar Ahmed Khan here explained How to create a Cascading DropDownLists in ASP.Net that is DropDownLists using jQuery and AJAX which depend on the previous or parent DropDownList for the data
In my previous article I explained Creating Cascading DropDownLists in ASP.Net. This article is extension of my previous article as here I am using jQuery JavaScript Library to make the User Interface pleasant by making use of JQuery AJAX for the Cascading DropDownList functionality
Cascading DropDownList means a series of dependent DropDownLists where one DropDownList is dependent on the parent or previous DropDownList and is populated based on the item selected by the user. On many occasions we need to make use of Cascading DropDownLists as I have here
Continent - Country - City
City is dependent on Country and in turn Country is dependent on the Continent thus we need a series of Cascading DropDownList here.
 
Database Design
For this tutorial I have created three tables whose designs are given below
Continents Table
Cascading DropDownlists using jquery and ajax
Countries Table
Cascading DropDownlists using jquery and ajax
Cities Table
Cascading DropDownlists using jquery and ajax
Connection String
<connectionStrings>
 <addname="conString"connectionString="Data Source=.\SQLEXPRESS;
    database=CascadingDDL;Integrated Security=true"/>
</connectionStrings>
 
Front End Design
I have used 3 DropDownLists one for each entity the mapping is shown below
1. ddlContinents - List of Continents
2. ddlCountries- List of Countries
3. ddlCities - List of Cities
 
Continents:<asp:DropDownList ID="ddlContinents" runat="server" AppendDataBoundItems="true"
             onchange = "PopulateContinents();">
    <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>                
</asp:DropDownList>
<br /><br />
Country:<asp:DropDownList ID="ddlCountries" runat="server"
             onchange = "PopulateCities();">
    <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>                
</asp:DropDownList>
<br /><br />
City:<asp:DropDownList ID="ddlCities" runat="server">
    <asp:ListItem Text = "Please select" Value = "0"></asp:ListItem>                
</asp:DropDownList>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick = "Submit" /> 

 
Populating the Continents DropDownList
On the Page_Load Event of the Page I am populating the Continents DropDownList
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.PopulateContinents();
    }
}
 
private void PopulateContinents()
{
    String strConnString = ConfigurationManager
        .ConnectionStrings["conString"].ConnectionString;
    String strQuery = "select ID, ContinentName from Continents";
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQuery;
            cmd.Connection = con;
            con.Open();
            ddlContinents.DataSource = cmd.ExecuteReader();
            ddlContinents.DataTextField = "ContinentName";
            ddlContinents.DataValueField = "ID";
            ddlContinents.DataBind();
            con.Close();
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.PopulateContinents()
        End If
End Sub
 
Private Sub PopulateContinents()
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim strQuery As String = "select ID, ContinentName from Continents"
        Dim con As SqlConnection = New SqlConnection(strConnString)
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        con.Open()
        ddlContinents.DataSource = cmd.ExecuteReader
        ddlContinents.DataTextField = "ContinentName"
        ddlContinents.DataValueField = "ID"
        ddlContinents.DataBind()
        con.Close()
End Sub
 
Populating Countries
I am populating the Countries DropDownList based on the ID of the Continent Selected by the user. The below method is declared as Web Method and Static (C#) Shared (VB) so that it can be called using jQuery.
For more information on calling server side methods using jQuery. Refer my article
C#
[System.Web.Services.WebMethod]
public static ArrayList PopulateCountries(int continentId)
{
    ArrayList list = new ArrayList();
    String strConnString = ConfigurationManager
        .ConnectionStrings["conString"].ConnectionString;
    String strQuery = "select ID, CountryName from Countries where ContinentID=@ContinentID";
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@ContinentID", continentId);
            cmd.CommandText = strQuery;
            cmd.Connection = con;
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                list.Add(new ListItem(
               sdr["CountryName"].ToString(),
               sdr["ID"].ToString()
                ));
            }
            con.Close();
            return list;
        }
    }
}
 
VB.Net
<System.Web.Services.WebMethod()> _
Public Shared Function PopulateCountries(ByVal continentId As Integer) As ArrayList
        Dim list As ArrayList = New ArrayList
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim strQuery As String = "select ID, CountryName from Countries where ContinentID=@ContinentID"
        Dim con As SqlConnection = New SqlConnection(strConnString)
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ContinentID", continentId)
        cmd.CommandText = strQuery
        cmd.Connection = con
        con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader
       While sdr.Read
            list.Add(New ListItem(sdr("CountryName").ToString, sdr("ID").ToString))
        End While
        con.Close()
        Return list
End Function

You will notice I am passing the ID of the continent as parameter to the Query using SelectedItemValue property of the DropDownList thus the query returns the records (Countries) for that Continent ID which are then bind to the Country DropDownList
 
Populating the Cities
Now on the selection of the Country I am filling the Cities for that country into the City DropDownList. The following method is also a Web Method so that the same came be called up using jQuery AJAX.
C#
[System.Web.Services.WebMethod]
public static ArrayList PopulateCities(int countryId)
{
    ArrayList list = new ArrayList();
    String strConnString = ConfigurationManager
        .ConnectionStrings["conString"].ConnectionString;
    String strQuery = "select ID, CityName from Cities where CountryID=@CountryID";
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@CountryID", countryId);
            cmd.CommandText = strQuery;
            cmd.Connection = con;
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                list.Add(new ListItem(
               sdr["CityName"].ToString(),
               sdr["ID"].ToString()
                ));
            }
            con.Close();
            return list;
        }
    }
}
 
VB.Net
<System.Web.Services.WebMethod()> _
    Public Shared Function PopulateCities(ByVal countryId As Integer) As ArrayList
        Dim list As ArrayList = New ArrayList
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
        Dim strQuery As String = "select ID, CityName from Cities where CountryID=@CountryID"
        Dim con As SqlConnection = New SqlConnection(strConnString)
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@CountryID", countryId)
        cmd.CommandText = strQuery
        cmd.Connection = con
        con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
            list.Add(New ListItem(sdr("CityName").ToString, sdr("ID").ToString))
        End While
        con.Close()
        Return list
End Function

Above I am firing a query on the Cities Table and getting all the cities that belong to that country which was selected by the user.
 
Populating Countries DropDownList Client Side
The following function gets called on the onchange event of Continents DropDownList. You will notice below that this function simply calls the PopulateCountries method with the ID of the selected Continent as parameter using jQuery AJAX that described earlier. And on OnSuccess event of the jQuery AJAX call the Countries DropDownList is populated. The pageUrl variable stores the URL of the page that will handle the jQuery AJAX calls. Note for VB.Net the URL will be set to VB.aspx
<script type = "text/javascript">
var pageUrl = '<%=ResolveUrl("~/CS.aspx")%>'
function PopulateContinents() {
    $("#<%=ddlCountries.ClientID%>").attr("disabled", "disabled");
    $("#<%=ddlCities.ClientID%>").attr("disabled", "disabled");
    if ($('#<%=ddlContinents.ClientID%>').val() == "0") {
        $('#<%=ddlCountries.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>');
        $('#<%=ddlCities.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>');
    }
    else {
        $('#<%=ddlCountries.ClientID %>').empty().append('<option selected="selected" value="0">Loading...</option>');
        $.ajax({
            type: "POST",
            url: pageUrl + '/PopulateCountries',
            data: '{continentId: ' + $('#<%=ddlContinents.ClientID%>').val() + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnCountriesPopulated,
            failure: function(response) {
                alert(response.d);
            }
        });
    }
}
 
function OnCountriesPopulated(response) {
    PopulateControl(response.d, $("#<%=ddlCountries.ClientID %>"));
}
</script>
 
Populating Cities DropDownList Client Side
In the similar way I am populating the Cities DropDownList on the onchange event of the Countries DropDownList, I am calling the PopulateCities method described earlier with selected country as the parameter. And on OnSuccess event of the jQuery AJAX call I am populating the cities DropDownList
<script type = "text/javascript">
function PopulateCities() {
    $("#<%=ddlCities.ClientID%>").attr("disabled", "disabled");
    if ($('#<%=ddlCountries.ClientID%>').val() == "0") {
        $('#<%=ddlCities.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>');
    }
    else {
        $('#<%=ddlCities.ClientID %>').empty().append('<option selected="selected" value="0">Loading...</option>');
        $.ajax({
            type: "POST",
            url: pageUrl + '/PopulateCities',
            data: '{countryId: ' + $('#<%=ddlCountries.ClientID%>').val() + '}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnCitiesPopulated,
            failure: function(response) {
                alert(response.d);
            }
        });
    }
}
 
function OnCitiesPopulated(response) {
    PopulateControl(response.d, $("#<%=ddlCities.ClientID %>"));
}
</script>
 
Populating the DropDownLists with data
The following method is used to populate the DropDownList with the data returned from the server. It accepts the list of data along with the reference of the control that needs to be populated.
<script type = "text/javascript">
function PopulateControl(list, control) {
    if (list.length > 0) {
        control.removeAttr("disabled");
        control.empty().append('<option selected="selected" value="0">Please select</option>');
        $.each(list, function() {
            control.append($("<option></option>").val(this['Value']).html(this['Text']));
        });
    }
    else {
        control.empty().append('<option selected="selected" value="0">Not available<option>');
    }
}
</script>

Fetching the Selections
On click event of button I am fetching the selected values server side. Since the Countries and Cities DropDownLists are populated client side their selections won’t be available directly and hence to get the values back we need to do the following
C#
protected void Submit(object sender, EventArgs e)
{
    string continent = Request.Form[ddlContinents.UniqueID];
    string country = Request.Form[ddlCountries.UniqueID];
    string city = Request.Form[ddlCities.UniqueID];
 
    // Repopulate Countries and Cities
    PopulateDropDownList(PopulateCountries(int.Parse(continent)), ddlCountries);
    PopulateDropDownList(PopulateCities(int.Parse(country)), ddlCities);
    ddlCountries.Items.FindByValue(country).Selected = true;
    ddlCities.Items.FindByValue(city).Selected = true;
}

VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
        Dim continent As String = Request.Form(ddlContinents.UniqueID)
        Dim country As String = Request.Form(ddlCountries.UniqueID)
        Dim city As String = Request.Form(ddlCities.UniqueID)
 
        ' Repopulate Countries and Cities
        PopulateDropDownList(PopulateCountries(Integer.Parse(continent)), ddlCountries)
        PopulateDropDownList(PopulateCities(Integer.Parse(country)), ddlCities)
        ddlCountries.Items.FindByValue(country).Selected = True
        ddlCities.Items.FindByValue(city).Selected = True
End Sub
 
Above you will notice I am again populating the DropDownLists and then selecting the values back again so that we can preserve the user selections across postbacks. Below is the method that populates the DropDownLists server side.
C#
private void PopulateDropDownList(ArrayList list, DropDownList ddl)
{
    ddl.DataSource = list;
    ddl.DataTextField = "Text";
    ddl.DataValueField = "Value";
    ddl.DataBind();
}
 
VB.Net
Private Sub PopulateDropDownList(ByVal list As ArrayList, ByVal ddl As DropDownList)
        ddl.DataSource = list
        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataBind()
End Sub

The output is shown in the screenshot below
Cascading Dropdownlists using jQuery and AJAX

Thus we come to the end of this article. Download the sample source code in VB.Net and C# using the link below
CascadingDropDownListsJQuery.zip

If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Ravindranath Rocks said:
I have used you kind of coding in my asp.net code but my problem is the other dropdown does not get populated. the reason is that it does not recognize the method PopulateSections. i have made the same changes as you did in your code. my code is as follows:br br var pageUrl ResolveUrl(QuestionAdmin.aspx)br function PopulateAllSections() br br if ((#ModuleSection.ClientID).val() 0) br br (#SectionDropdown.ClientID ).empty().append(option selectedselected value0Please selectoption)br br else br br (#SectionDropdown.ClientID ).empty().append(option selectedselected value0Loading...option)br .ajax(br type: POSTbr url: pageUrl PopulateSectionsbr data: ItemID: (#ModuleSection.ClientID).val() br contentType: applicationjson charsetutf-8br dataType: jsonbr success: OnCountriesPopulatedbr failure: function(response) br br alert(response.d)br br )br br br br please suggest me the answer ASAP. br Thanks in advance.
March 31, 2010  

Mudassar Khan said:
Reply To: Ravindranath Rocks
I can't say how you merged your code with mine its better that you first try out with a sample standalone application and then fit it in your application. Else post your code on asp.net forums
March 31, 2010  

ravindranath rocks said:
Thanks mudassar i have successfully implemented my code. Actually i was hitting a ascx page and my function was in the ascx page. The jquery does not hit the ascx function since it does not support page methods.. Thanks for your reply and thanks for the beautiful code you wrote. It helped me a lot.
April 01, 2010  

Jacky said:
It is a great article.But when the form is postbackand on page reload the values in the child dropdown list along with the selected value will be lost.How can we solve this issue Thank you.
April 16, 2010  

Mudassar Khan said:
Reply To: Jacky
Thanks Jacky for pointing out the issue. I have updated the article and also the attached sample.
April 17, 2010  

Jacky said:
Thank you for your prompt reply Mudassar. I actually applied it to my code and it worked perfectly.
April 18, 2010  

niki said:
hibr im have master page and use this(AJAX Cascading DropDownList using jQuery in ASP.Net ) in details page . now this not work in.br please help me
June 26, 2010  

Mudassar Khan said:
Reply To: niki
It is not working since it is not able to find the Controls in DetailsView

Hence it should be coded this way

('#<%=DetailsView1.FindControl("ddlCountries").ClientID%>')
June 26, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code