Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having three dropdowns,

in first dropdown i am binding values from db directly,

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" CssClass="DropDown"
                        Width="190px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    </asp:DropDownList>


C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDown1ListView();
        }

    }


C#
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---");
        }
    }


C#
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.NET
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" CssClass="DropDown"
                       Width="190px" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
                   </asp:DropDownList>


C#
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---");
        }
    }


C#
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.NET
<asp:DropDownList ID="DropDown3" runat="server" CssClass="DropDown" Width="190px">
                    </asp:DropDownList>


C#
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
Posted
Comments
Karthik_Mahalingam 10-Jan-14 7:14am    
r u getting any error ?
riodejenris14 10-Jan-14 7:16am    
no error karthik but dropdown2 values is not getting selected as in which i clicked.
Gandalf_TheWhite 10-Jan-14 7:16am    
Do you have update panel in your page?
riodejenris14 10-Jan-14 7:18am    
Yes i am having update panel Gandalf - The White_.
Debug and see where is the issue.

1 solution

What you can try is to remove the update panel and set the page event EnableEventValidation to False
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900