Click here to Skip to main content
15,440,771 members
Articles / DevOps / Testing
Posted 11 Oct 2013

Tagged as


1 bookmarked

CascadingDropDown in Ajax With

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL3 min read
Step1: Create a table in database:- create table test( state1 varchar(50),city varchar(50)  )  Step2:- In the .aspx Page:-   <select id="statelist"

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Step1: Create a table in database:-


create table test


 state1 varchar(50),

city varchar(50)  


Step2:- In the .aspx Page:-


  <select id="statelist" runat="server" onchange="showCity()" style="width: 160px">



                        <asp:DropDownList ID="citylist" runat="server" Width="184px">




Take statelist for State and citylist for City 

When you select any item from statelist the citylist will automatically fill because of ajax 


Step3:-  Write this in the <head> Part:-

  <script language="JavaScript" type="text/javascript" >

var xmlHttp

var arr;

function showCity()



var url="Default.aspx"




return false;


function stateChanged() 

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

            var str;

            str = xmlHttp.responseText;



           var i;



               dd2= document.getElementById("citylist")


                var theOption = new Option;

                theOption.text = arr[i];

                theOption.value = arr[i];

                dd2.options[i] = theOption;


function GetXmlHttpObject()

var objXMLHttp=null

if (window.XMLHttpRequest)


objXMLHttp=new XMLHttpRequest()


else if (window.ActiveXObject)


objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")


return objXMLHttp




Step 4:-  in the .cs page:-


SqlConnection con = new SqlConnection("data source = MY\\SQLEXPRESS; initial catalog = try; integrated security=true");

    protected void Page_Load(object sender, EventArgs e)


        if (!Page.IsPostBack)



            SqlCommand cmd = new SqlCommand("select * from test", con);

            SqlDataReader dr;

            dr = cmd.ExecuteReader();

            statelist.DataSource = dr;

            statelist.DataTextField = "state";





        if (Request.QueryString["name"] != null)


            string name = Request.QueryString["name"];



            SqlCommand cmd = new SqlCommand("select * from test where state=@state", con);

            cmd.Parameters.AddWithValue("@state", name);

            SqlDataReader dr;

            dr = cmd.ExecuteReader();

            string str = "";

            int a = 0;

            while (dr.Read())


                if (a == 0)

                    str += dr["city"].ToString();


                    str += "," + dr["city"].ToString();









This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

757 members

Comments and Discussions

-- There are no messages in this forum --