ASP.Net dropdownlist不会返回selectedindexchanged事件的索引

我有一个dropdownlist控件,可以正确触发selectedindexchanged事件。 但是,从列表中选择项目时,返回到selectedindexchanged事件的索引值不会更改; 列表框会弹出列表中的第一项。 任何帮助,将不胜感激。 〜苏珊〜

ASP下降列表控制

 <asp:DropDownList ID="CuisineList" runat="server" Width="100" 
    AutoPostBack = "true" onselectedindexchanged="CuisineList_SelectedIndexChanged" >
 </asp:DropDownList>

数据绑定以控制PAGELOAD事件:

 protected void Page_Load(object sender, EventArgs e)
 {
    //Load drop down lists for restaurant selection

    BLgetMasterData obj = new BLgetMasterData();

    var cusineList = obj.getCuisines();
    CuisineList.DataSource = cusineList;
    CuisineList.DataBind();
    CuisineList.Items.Insert(0, "Any");

SELECTEDINDEXCHANGEDEVENT:

   protected void CuisineList_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (IsPostBack)
        {
            string def = this.CuisineList.SelectedItem.Value;
            //aLWAYS returns index '0'
        }
    }

您需要将您的Dropdownlist绑定代码置于!IsPostBack()中的page_load事件中。 喜欢...

protected void Page_Load(object sender, EventArgs e) { 
 if(!IsPostBack)
 {
  BLgetMasterData obj = new BLgetMasterData();

  var cusineList = obj.getCuisines();
  CuisineList.DataSource = cusineList;
  CuisineList.DataBind();
  CuisineList.Items.Insert(0, "Any");
 }
}

原因:每当你的SelectedIndex更改事件触发时, Page_load事件在之前调用SelectedIndex变化事件,您的下拉数据rebinded ,这就是为什么你currect selection was lost

链接地址: http://www.djcxy.com/p/67227.html

上一篇: ASP.Net dropdownlist not returning index on selectedindexchanged event

下一篇: asp.net DropDownList event not firing