Sie sind auf Seite 1von 3

Populate Cascading DropDownList based on selection of other dropdown ASP.

NET
Posted by amiT jaiN at 10:59 PM There are several situations where we need to populate second or third dropdown list based on selection of first or second dropdwon. For example populating state DropDown based on selection of Country from Country DropDown list and then population Cities DropDown based on selectedIndex of State For this we need to write code in code behind in the SelectedIndexChanged Event of respective Dropdown to implement the cascading of DropDowns The html source of the page goes like this <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <strong>Country : &nbsp;</strong> <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"> </asp:DropDownList><br /> <br /> <strong>State: &nbsp; &nbsp; &nbsp; </strong> <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"> </asp:DropDownList><br /> <br /> <strong>City: &nbsp; &nbsp; &nbsp;&nbsp; </strong> <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True"> </asp:DropDownList><br /> <br /> <asp:Label ID="lblMsg" runat="server"></asp:Label></div> </form> </body> </html>

And the code behind for this will be protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillCountry(); } } protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { int CountryID = Convert.ToInt32(ddlCountry.SelectedValue.ToString()); FillStates(CountryID);

} protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { int StateID = Convert.ToInt32(ddlState.SelectedValue.ToString()); FillCities(StateID); } private void FillCountry() { string strConn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select CountryID, Country from Country"; DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { ddlCountry.DataSource = objDs.Tables[0]; ddlCountry.DataTextField = "Country"; ddlCountry.DataValueField = "CountryID"; ddlCountry.DataBind(); ddlCountry.Items.Insert(0, "--Select--"); } else { lblMsg.Text = "No Countries found"; } } private void FillStates(int countryID) { string strConn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select StateID, State from State where CountryID =@CountryID"; cmd.Parameters.AddWithValue("@CountryID", countryID); DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { ddlState.DataSource = objDs.Tables[0]; ddlState.DataTextField = "State"; ddlState.DataValueField = "StateID"; ddlState.DataBind(); ddlState.Items.Insert(0, "--Select--"); } else { lblMsg.Text = "No states found";

} } private void FillCities(int stateID) { string strConn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select CityID, City from City where StateID =@StateID"; cmd.Parameters.AddWithValue("@StateID", stateID); DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { ddlCity.DataSource = objDs.Tables[0]; ddlCity.DataTextField = "City"; ddlCity.DataValueField = "CItyID"; ddlCity.DataBind(); ddlCity.Items.Insert(0, "--Select--"); } else { lblMsg.Text = "No Cities found"; } }

Das könnte Ihnen auch gefallen