[英]Selected Item not being updated?
I've got the following DropDownList control: 我有以下DropDownList控件:
<asp:DropDownList ID="SubjectFilter" runat="server" AutoPostBack="True" onselectedindexchanged="SubjectFilter_SelectedIndexChanged"></asp:DropDownList>
SubjectFilter
data: SubjectFilter
数据:
BookStore b = new BookStore();
b.LoadFromXML(Server.MapPath("list.xml"));
SubjectFilter.DataSource = b.BooksList.Select(x => x.Subject).Distinct().ToArray();
SubjectFilter.DataBind();
SubjectFilter.Items.Insert(0, new ListItem("הכל", "Default"));
Everything loads just fine. 一切都很好。 However in the
SubjectFilter_SelectedIndexChanged
method, SubjectFilter.SelectedValue
is always Default
, even though I'm selecting different options. 但是,在
SubjectFilter_SelectedIndexChanged
方法中,即使我选择其他选项, SubjectFilter.SelectedValue
始终为Default
。
What is the problem? 问题是什么? Thank you very much.
非常感谢你。
I'm guessing the above code is from the PageLoad
event. 我猜上面的代码是从
PageLoad
事件。 You may want to wrap that in a if(!isPostBack)
block. 您可能需要将其包装在
if(!isPostBack)
块中。
Make sure that in your Page_Load
that you only populate the dropdown when IsPostBack
is false. 确保在
Page_Load
,仅当IsPostBack
为false时才填充下拉列表。
For example 例如
public void Page_Load(...)
{
if (!IsPostback())
UpdateDisplay();
}
When are you binding the drop down? 您何时绑定下拉列表? You may any to wrap in an If(page.ispostback ==false) It looks like you may be binding on page load, before you check its value..
您可以将任何包装在If(page.ispostback == false)中。在您检查其值之前,似乎在页面加载时就进行了绑定。
ViewState is assigned between the Init and Load for an ASP.NET page. ViewState在ASP.NET页的“初始化”和“加载”之间分配。 Your event handlers occur after load.
您的事件处理程序在加载后发生。 If you are programmatically setting content in the controls that your user will be using, you want to handle that before ViewState is applied.
如果要通过编程方式在用户将使用的控件中设置内容,则需要在应用ViewState 之前进行处理。 In other words, move it to Page_Init.
换句话说,将其移动到Page_Init。 Afterwards, ViewState kicks in and you'll see what the user actually selected when your handler executes.
之后,将启动ViewState,您将看到执行处理程序时用户实际选择的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.