asp.netdata-bindingdropdownautopostback

When selecting value from dropdown list, it is resetting all other dropdown values


When selecting value from country drop-down list it is resetting the value of all other drop-down list boxes and the selected country is also getting reset.

The country drop-down list, state drop-down list and district drop-down list are dependent.

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
            cmd.Connection = con;
            con.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            con.Close();
        }
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}

protected void gCountry2_SelectedIndexChanged(object sender, EventArgs e)
{
    gState2.Enabled = false;
    gDistrict2.Enabled = false;
    gState2.Items.Clear();
    gDistrict2.Items.Clear();
    //gState2.Items.Insert(0, new ListItem("Select State", "0"));
    //gDistrict2.Items.Insert(0, new ListItem("Select City", "0"));
    int countryId = int.Parse(gCountry2.SelectedItem.Value);
    if (countryId > 0)
    {
        string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId);
        BindDropDownList(gState2, query, "StateName", "StateId", "Select State");
        gState2.Enabled = true;
        Page.SetFocus(f2.ClientID);
    }
}
protected void gState2_SelectedIndexChanged1(object sender, EventArgs e)
{
    gDistrict2.Enabled = false;
    gDistrict2.Items.Clear();
    //gDistrict2.Items.Insert(0, new ListItem("Select City", "0"));
    int stateId = int.Parse(gState2.SelectedItem.Value);
    if (stateId > 0)
    {
        string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId);
        BindDropDownList(gDistrict2, query, "CityName", "CityId", "Select City");
        gDistrict2.Enabled = true;
        Page.SetFocus(f2.ClientID);          
}

Solution

  • There is much easier way to do what you want. Bind dropdowns declaratively. Something like this.

    <%-- .aspx file. --%>
    <%-- populate country DDL --%>
    <asp:DropDownList ID="ddCountry" runat="server" DataTextField="countryName"
        DataValueField="countryId"
        AppendDataBoundItems="true" DataSourceID="sqlCountry" AutoPostBack="true">
        <asp:ListItem Value="0" Text="- Select Country -"></asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="sqlCountry" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
        SelectCommand="select countryid,countryname from dbo.countries">
    </asp:SqlDataSource>
    
    <%-- populate state DDL --%>
    <asp:DropDownList ID="ddState" runat="server" DataTextField="StateName"
        DataValueField="stateId"
       DataSourceID="sqlState" AutoPostBack="true" OnDataBound="ddState_DataBound">
    </asp:DropDownList>
    <asp:SqlDataSource ID="sqlState" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
        SelectCommand="select stateId, stateName from dbo.states where countryId=@countryId">
        <SelectParameters>
            <%-- Bind parameter to the parent control --%>
            <asp:ControlParameter DefaultValue="0" ControlID="ddCountry" PropertyName="SelectedValue" Name="countryId" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    
    <%-- populate district DDL --%>
    <asp:DropDownList ID="ddDistrict" runat="server" DataTextField="CityName"
        DataValueField="cityId"
       DataSourceID="sqlDistrict" AutoPostBack="true" OnDataBound="ddDistrict_DataBound">
    </asp:DropDownList>
    <asp:SqlDataSource ID="sqlDistrict" runat="server" ConnectionString="<%$ ConnectionStrings:ConnString %>"
        SelectCommand="select cityId, cityName from dbo.cities where stateId=@stateId">
        <SelectParameters>
            <asp:ControlParameter DefaultValue="0" ControlID="ddState" PropertyName="SelectedValue" Name="stateId" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    

    You only need code behind to add "-- Select --" items to two last dropdowns.

    // .aspx.cs
    protected void ddState_DataBound(object sender, EventArgs e)
    {
        ddState.Items.Insert(0, new ListItem("--Select State--", "0"));
    }
    protected void ddDistrict_DataBound(object sender, EventArgs e)
    {
        ddDistrict.Items.Insert(0, new ListItem("--Select City--", "0"));
    }