[英]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.