How to Bind the Dependent dropdownlist control?
How to Bind the Dependent dropdownlist control? In this article i am going to explain you the process of binding the dropdownlist control which is dependent on each other. We will take 3 dropdons which dependts on each other.
We will discuss the very common Country,state,city scenario.
In this city dropdown will be dependent on the state dropdown and state dropdown will be dependent on the
country dropdown.
We first take 3 dropdown like:
<asp:DropDownList ID="ddl_country" runat="server" AutoPostBack="True"
DataTextField="Country" DataValueField="CountryID" class="txtbkcolor"
OnSelectedIndexChanged="ddl_country_SelectedIndexChanged">
<asp:ListItem Text="Select Country" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_state" runat="server" AutoPostBack="True"
DataTextField="State" DataValueField="StateID" class="txtbkcolor"
OnSelectedIndexChanged="ddl_state_SelectedIndexChanged">
<asp:ListItem Text="Select State" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_city" runat="server" AutoPostBack="True"
class="txtbkcolor"
DataTextField="City"
onselectedindexchanged="ddl_city_SelectedIndexChanged">
<asp:ListItem Text="Select City " Value="0"></asp:ListItem>
</asp:DropDownList>
now in code behind we will have below code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CountryBind();
}
}
//this will bind Country dropdown
protected void CountryBind()
{
string sqlQuery = "Select * from Country";
SqlConnection conn = new SqlConnection(c);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddl_country.DataSource = ds;
ddl_country.DataBind();
conn.Close();
}
//this will bind State dropdown
protected void StateBind()
{
string sqlQuery = "SELECT * FROM State " + " WHERE (CountryID = " + Convert.ToInt32(ddl_country.SelectedValue) + " )";
SqlConnection conn = new SqlConnection(c);
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddl_state.DataSource = ds;
ddl_state.DataBind();
conn.Close();
}
}
//this will bind City dropdown
protected void CityBind()
{
string sqlQuery = "SELECT * FROM City " + " WHERE (StateID = " + Convert.ToInt32(ddl_state.SelectedValue) + " and CountryID=" + Convert.ToInt32(ddl_country.SelectedValue) + ")";
SqlConnection conn = new SqlConnection(c);
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
da.Fill(ds);
ddl_city.DataSource = ds;
ddl_city.DataBind();
conn.Close();
}
}
protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e)
{
StateBind();
}
protected void ddl_state_SelectedIndexChanged(object sender, EventArgs e)
{
CityBind();
}
protected void ddl_city_SelectedIndexChanged(object sender, EventArgs e)
{
}
This is article are good but some changes .This code to every time to select index changed event fire then page are refresh .But you can use update panel then some items are refresh not a full page are refresh