ASP.NET DropDownList SelectedItem.Value不变 [英] Asp.NET DropDownList SelectedItem.Value not changing
本文介绍了ASP.NET DropDownList SelectedItem.Value不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
标记:
<div style="float:left;margin-top:15px;width:80px">
<asp:DropDownList ID="MyList" runat="server" Width="100px"></asp:DropDownList>
</div>
代码:
// clear vehicles list
MyList.Items.Clear();
// add 'all' option
MyList.Items.Add(new ListItem("ALL", "0"));
// add assets
foreach (CustomClass item in items)
MyList.Items.Add(new ListItem(item.Name, item.ID.ToString()));
由于没有必要,因此没有事件触发SelectedIndexChanged。
No event triggering for SelectedIndexChanged since it's not necessary.
当我单击用于回发的按钮时,selecteditem的值仍然是DropDownList中第一项的值。我想念什么?
When I click the button for postback, the value of the selecteditem remains the value of the first item in the DropDownList. What am I missing?
注意
请停止回复和编辑帖子。
NOTE Please stop replying and editing posts. We may leave it as it is since it has been answered already.
推荐答案
如果要在 Page_Load
,实际上您还需要重置SelectedItem。
If you're databinding in Page_Load
, you're essentially also resetting the SelectedItem.
您应该包装 Page_Load中存在的任何绑定代码。
在 if(!IsPostBack)
块中。
if(!Page.IsPostBack)
{
// Your binding code here ...
}
这篇关于ASP.NET DropDownList SelectedItem.Value不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文