从下拉列表中选择值时,它正在重置所有其他下拉值

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

当从国家/地区下拉列表中选择值时,它会重置所有其他下拉列表框的值,并且所选国家/地区也会重置。

国家下拉列表、州下拉列表和地区下拉列表是相关的。

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);          
}

有更简单的方法来做你想做的事。以声明方式绑定下拉菜单。像这样。

<%-- .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>

您只需要隐藏代码即可将“-- Select --”项添加到最后两个下拉列表中。

// .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"));
}