I am having three dropdowns,
in first dropdown i am binding values from db directly,
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" CssClass="DropDown"
Width="190px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDown1ListView();
}
}
private void DropDown1ListView()
{
SqlConnection pCnn = new SqlConnection(Connection.Con);
SqlDataAdapter da = new SqlDataAdapter("exec usp_dropdownlevel", pCnn);
DataSet ds = new DataSet();
pCnn.Open();
da.Fill(ds);
pCnn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "---Select---");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string level = DropDownList1.SelectedItem.ToString();
DropDown2(level);
DropDownList3.SelectedIndex = 0;
}
To bind second dropdown i have to get data from dropdown1
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" CssClass="DropDown"
Width="190px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
private void DropDownList2(string level)
{
SqlConnection pCnn = new SqlConnection(Connection.Con);
SqlDataAdapter da = new SqlDataAdapter("exec usp_dropdownarea'" + level + "'", pCnn);
DataSet ds = new DataSet();
pCnn.Open();
da.Fill(ds);
pCnn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
DropDownList2.DataSource = ds.Tables[0];
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Code";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, "---Select Area---");
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string area = DropDownList2.SelectedItem.ToString();
DropDown3(area);
}
To bind third dropdown i have to get data from dropdown2
<asp:DropDownList ID="DropDown3" runat="server" CssClass="DropDown" Width="190px">
</asp:DropDownList>
private void DropDown3(string area)
{
SqlConnection pCnn = new SqlConnection(Connection.Con);
SqlDataAdapter da = new SqlDataAdapter("exec usp_DropDown3'" + area + "'", pCnn);
DataSet ds = new DataSet();
pCnn.Open();
da.Fill(ds);
pCnn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
DropDown3.DataSource = ds.Tables[0];
DropDown3.DataTextField = "Name";
DropDown3.DataValueField = "Code";
DropDown3.DataBind();
DropDown3.Items.Insert(0, "---Select Subject---");
}
}
The issue in this code is dropdown 1 is binding correctly but selecting values in dropdown2 is not changing, first value of the dropdown2 is getting select automatically even if i select some other values from dropdown2