• Home
  • About
  • BestBloggingIdeas
  • DotNetLearningSource
  • FORUM
  • Joblinks
  • Latest News
  • Policy
  • POSTS
  • SimplySqlServer.Com && SimplyAspDotNet.Com
  • Sitemap

Join Ours Forum

Asp.Net,C#,Ajax,Sql server,silverlight,Javascript codes exambles articles,Programming exambles

RSS Feed
  • Bounty Huge Roll [Amazon Frustration-Free Packaging]
  • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
  • The Basic Overview of Windows Mobile Development Asp.Net C#
  • Overview of Sql server extended properties Asp.Net C#
  • How to Use Sql Server Extended properties using visual studio Asp.Net C#
  • Adobe Dreamweaver Templates Accelerate Web Development
  • Top Tips for Web Design Projects
  • How to Achieve a Good Web Design Structure
  • To Use Or Not To Use Website Templates
  • Five Tips to a Successful Website
  • Top 10 Articles,


    Silverlight Datagrid Select Update Delete Insert Asp.Net C#

    Differences Similarities Benefits Between Typed Datasets and Untyped Datasets asp.net c#

    Linq to Sql Introduction Entities Ado.Net C# SqlClasses Attributes Linq Mapping

    Linq Programming/How Linq Works?/Linq Implementation In Asp.Net C# Ado.Net

    Performing Developing Using Investigating Asp.Net 2.0 Ajax Application Development Asp.Net C#

    Hosting/Install Wcf Services in a Windows Service Asp.Net C#

    Connecting Silverlight to Wcf Asp.Net C#

    Silverlight Data Grid Data Binding WCF Asp.Net C#

    Invoking/Accessing/Calling WCF Service Without Adding/Creating Proxy/Reference Asp.Net C#

    Performing Doing Creating Insert Update Delete sql data Using Linq Database Asp.Net C#

    Creating Developing Cascading DropDownlist Using Ajax/JQuery/JavaScript/ASP.NET

    Posted by on March 27, 2010 Leave a comment (10) Go to comments

    Introduction:
    In this article,i am going to explain about how to create a cascading dropdownlist using ajax/javascript.

    Main:

    What is Cascading DropDown List?

    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.

    See the below examble,

    In default.aspx page,

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CS.aspx.cs" Inherits="_Default" EnableEventValidation = "false" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <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>
    <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>
    <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>
    </head>
    <body>
        <form id="form1" runat="server">
    <div>
    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" />                
    </div>
        </form>
    </body>
    </html>

    In default.aspx.cs page,

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections;
     
    public partial class _Default : System.Web.UI.Page 
    {
    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();
            }
        }
    }
     
    [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;
            }
        }
    }
     
    [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;
            }
        }
    }
     
    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];
    }
    }

    For Downloading the jquery script file,just click jquery2

    Conclusion:
    Hope this helps,
    Happy Coding.

    JAVASCRIPT
    ← Accessing Getting Using ViewState Data Across Several WebPages/Aspx Pages Using Asp.Net/c#
    How to Bind a XML Node Value/Data Into ASP.NET DropDownList →

    Learn Easily Using Video Tutorials


    How to choose the right Java IDE – explained Eclipse NetBeans BlueJ

    Developing/Creating/Performing/Configuring Java Applications Using Eclipse IDE

    Step By Step Guide for Download/Install Configure Eclipse IDE for Java

    Editing data with the GridView control Asp.Net C#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Registering/Configuring Web Controls globally in web.config file asp.net c#

    Best way to prepare asp.net Interview - Success Stories

    Download Important Questions and PPT's:

    Sql Server Important Questions Online free download

    Dotnet Important Questions Online free download

    Exploring Linq to Sql Process Flow

    Learn how to perform silverlight programming

    Learn OOPs concepts in better and well manner

    Learn Ajax in better and well manner

    Leave a comment

    10 Comments.

    1. dental hygienist April 27, 2010 at 4:15 am

      nice post. thanks.

    2. tiffany jewellery May 2, 2010 at 5:52 pm

      Thank you for useful info. :-)

    3. mbt shoes May 7, 2010 at 5:43 am

      I like that you

      think. Thank you for share very

    4. Download Internet Explorer June 1, 2010 at 8:05 pm

      thanks for great informations It’s a wonderful
      Download Internet Explorer 8

    5. travel vacation packages June 1, 2010 at 9:54 pm

      I like that you

      think. Thank you for share very

    6. Jolynn Rudesill August 15, 2010 at 8:49 am

      Hi there, I found your blog via Google while searching for first aid for a heart attack and your post looks very interesting for me.

    7. homepage August 23, 2010 at 10:30 am

      Lots of great information and inspiration, both of which we all need, thanks for this.

    8. Developer November 1, 2010 at 1:26 pm

      Awesome!, Thanks!

    9. yasser December 16, 2010 at 11:41 am

      thanks alot but this example didn’t work with me can you help me

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    *

    *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Trackbacks and Pingbacks:

    • Webmaster Crap » Blog Archive » Creating Developing Cascading DropDownlist Using Ajax/JQuery … - Pingback on 2010/04/02/ 01:42

    Enter your email address:

    Delivered by FeedBurner

    • Recent Posts

      • Bounty Huge Roll [Amazon Frustration-Free Packaging]
      • XML Introduction to XML VHS Video Training, 1 hr., 32 minutes.
      • The Basic Overview of Windows Mobile Development Asp.Net C#
      • Overview of Sql server extended properties Asp.Net C#
      • How to Use Sql Server Extended properties using visual studio Asp.Net C#
    • Search by Tags!

      Application AspNet Basic between Black Bluetooth Build Business Collection Consultants Design Development Downloading effective Excel Experts Generics Implement Installing Interview Logic Management Microsoft Minutes Object Outlook Professional Programmer Programming Project Projects Questions Ready Select Server Services Silverlight Source Strings Studio Through using Visual Website Wordpress
    • Archives

      • August 2011
      • June 2011
      • May 2011
      • April 2011
      • March 2011
      • February 2011
      • December 2010
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • July 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • September 2009

    Copyright © 2012 NetProgrammingHelp.com

    Δ Top