简体   繁体   English

ASP.Net下拉列表没有返回selectedindexchanged事件的索引

[英]ASP.Net dropdownlist not returning index on selectedindexchanged event

I have a dropdownlist control with a selectedindexchanged event that fires correctly . 我有一个dropdownlist控件,其中有一个正确触发的selectedindexchanged 事件 However, when you select an item from the list the index value that is returned to the selectedindexchanged event does not change; 但是,从列表中选择项目时,返回到selectedindexchanged事件的索引值不会更改; the list box pops back tot he first item in the list. 列表框弹出列表中的第一个项目。 Any help would be appreciated. 任何帮助,将不胜感激。 ~susan~ 〜苏珊〜

ASP DROP DOWN LIST CONTROL : ASP DROP DOWN LIST控件

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

DATA BOUND TO CONTROL IN PAGELOAD EVENT: 在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: SELECTEDINDEXCHANGEDEVENT:

   protected void CuisineList_SelectedIndexChanged(object sender, EventArgs e)
    {

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

You need to put your Dropdownlist binding code under !IsPostBack() in the page_load event. 您需要将您的Dropdownlist绑定代码放在page_load事件中的!IsPostBack()下。 like... 喜欢...

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");
 }
}

Reason: Whenever your SelectedIndex Change Event fires, the Page_load event is called before the SelectedIndex Change event and your dropdown data rebinded , that's why your currect selection was lost . 原因:每当你的SelectedIndex更改事件触发时, Page_load事件在之前调用SelectedIndex变化事件,您的下拉数据rebinded ,这就是为什么你currect selection was lost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net dropdownlist的第一个选择不会触发SelectedIndexChanged事件 - asp.net dropdownlist first selection does not fire SelectedIndexChanged event ASP.Net DropDownList SelectedIndexChanged事件触发但什么都不做 - ASP.Net DropDownList SelectedIndexChanged Event fires but does nothing ASP.NET / C#:DropDownList SelectedIndexChanged事件未触发 - ASP.NET / C#: DropDownList SelectedIndexChanged event not firing ASP.NET MVC 5 DropDownList selectedindexchanged - ASP.NET MVC 5 DropDownList selectedindexchanged 嵌套的ASP.NET DropDownList SelectedIndexChanged未触发 - Nested ASP.NET DropDownList SelectedIndexChanged Not Firing ASP.NET DropDownList未在SelectedIndexChanged上绑定SQL - ASP.NET DropDownList not binding SQL on SelectedIndexChanged ASP.NET 下拉列表 selectedIndexChanged 没有被触发 - ASP.NET dropdownlist selectedIndexChanged not getting triggered ASP.NET DropDownList SelectedIndexChanged与UpdatePanel AsyncPostBackTrigger - ASP.NET DropDownList SelectedIndexChanged with UpdatePanel AsyncPostBackTrigger ASP.NET MVC 4 DropDownList selectedindexchanged - ASP.NET MVC 4 DropDownList selectedindexchanged 在asp.net的SelectedIndexChanged事件中获取动态创建的下拉列表的ID - get the id of the dynamically created dropdownlist in its SelectedIndexChanged event in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM