2017-05-28 63 views
0

我在我的網頁上有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的第一個值。 我該如何解決這個問題。對不起,我的英語不好。

+0

請後面顯示你的代碼。 –

+0

#Bob Swager問題更新了我的.cs文件的代碼 –

+0

https://stackoverflow.com/questions/3496456/setting-dropdownlist-selecteditem-programmatically 這個問題是類似的。 –

回答

0

沒有查看您的Page_Load事件,最有可能的是您在每次回發中運行下拉列表的初始填充。 您需要做的是使用Page.IsPostBack檢查頁面加載事件中的初始加載情況,該頁面告訴您頁面是否第一次訪問服務器。

private void Page_Load() 
{ 
    if (!Page.IsPostBack) 
    { 
     YourPopulationOfDdFunction(); 
    } 
} 
0
Narender Godara! i think the issue is when u call first drop down on page load u have to use condition 
    if(!ispostback) 
    { 
    //code of first dropdown %states 
    } 
    then u have to write the next code in the selectedindexchanged of first dropdown and so on.... 
by the way your english is better than many developers of aisa, so bro keep it up, stay blessed, 
#Good Luck 
+0

希望這樣做 – Haroon

相關問題