簡體   English   中英

在ASP中回發后,DropDownList所選索引已更改

[英]DropDownList selected index changed after postback in asp

我的網頁上有3個DropDownList,第一個是州,第二個是區,第三個是城市。

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

背后的代碼

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 =@statename and Districtname=@districtname 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)
        {
        }
    }

我已經在頁面加載時從數據庫填充了狀態,如果用戶選擇了任何狀態,則在selectedindexchanged事件上加載了所選狀態的所有區域。 但是問題是,如果用戶選擇任何狀態selectedindexchanged ,則會更改事件觸發和回發。 回發DropDownList之后,每次選擇ddl的第一個值。 我怎么解決這個問題。 對不起,我的英語不好。

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

private void Page_Load()
{
    if (!Page.IsPostBack)
    {
        YourPopulationOfDdFunction();
    }
}
    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM