2017-05-28 77 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

相关问题