我在我的網頁上有3個DropDownList,分別是State,Second for Districts和Third for Cities。背後DropDownList選定的索引在asp回發後發生變化
protected void districtddl_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlConnection con2 = new SqlConnection("my connection");
SqlCommand com = new SqlCommand("select DISTINCT Districtname,location from indiapincodes where statename [email protected] and [email protected] order by Districtname", con2);
SqlDataAdapter adpt = new SqlDataAdapter(com);
com.Parameters.AddWithValue("@statename", stateddl.SelectedValue.ToString());
com.Parameters.AddWithValue("@districtname", districtddl.SelectedValue.ToString());
DataTable dt2 = new DataTable();
adpt.Fill(dt2);
cityddl.DataSource = dt2;
cityddl.DataBind();
cityddl.DataTextField = "location";
cityddl.DataBind();
con2.Close();
con2.Dispose();
}
catch (Exception ex)
{
}
}
<asp:DropDownList ID="stateddl" OnSelectedIndexChanged="stateddl_SelectedIndexChanged" AutoPostBack="true" runat="server" CssClass="form-control"></asp:DropDownList>
Select District:
<asp:DropDownList ID="districtddl" OnSelectedIndexChanged="districtddl_SelectedIndexChanged" AutoPostBack="true" runat="server" CssClass="form-control" ViewStateMode="Enabled"></asp:DropDownList>
Select Area:
<asp:DropDownList ID="cityddl" runat="server" CssClass="form-control"></asp:DropDownList>
代碼中,我從頁面加載數據庫填充狀態,如果用戶選擇那麼任何狀態上加載selectedindexchanged
事件選擇的狀態的所有地區。 但問題是,如果用戶選擇任何狀態selectedindexchanged
事件觸發和回發。在回發DropDownList之後,每次選擇ddl的第一個值。 我該如何解決這個問題。對不起,我的英語不好。
請後面顯示你的代碼。 –
#Bob Swager問題更新了我的.cs文件的代碼 –
https://stackoverflow.com/questions/3496456/setting-dropdownlist-selecteditem-programmatically 這個問題是類似的。 –