1

我有一个下拉列表,我在 aspx 标记上声明如下:

<asp:DropDownList ID="State" runat="server"></asp:DropDownList>

然后我像这样在页面加载时绑定它:

protected void Page_Load(object sender, EventArgs e)
    {
       BindDropdowns();
    }
private void BindDropdowns()
    {
        State.DataSource = DataAccess.GetStates();
        State.DataValueField = "FieldId";
        State.DataTextField = "FieldName";
        State.DataBind();
    }

回发后不保留所选值,我也无法触发 selectedindexchangedevent。怎么了 ?

4

2 回答 2

5

请像这样更改您的代码:

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostback)
       BindDropdowns();
}

这意味着您的下拉控件仅在第一次页面加载时绑定一次

于 2012-04-30T20:58:40.317 回答
2

你必须使用AutoPostBack="true".

<asp:DropDownList ID="State" AutoPostBack="true" 
 runat="server"></asp:DropDownList>

并声明女巫事件处理程序是这样的:

<asp:DropDownList ID="State" AutoPostBack="true" 
OnSelectedIndexChanged="State_SelectedIndexChanged" 
runat="server"></asp:DropDownList>

然后在代码中。仅在不回发时绑定:

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostback)
       BindDropdowns();
}

protected void State_SelectedIndexChanged(object sender, System.EventArgs e)  
{  
    var somevalue= State.SelectedValue;  
} 
于 2012-04-30T21:02:50.880 回答