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