所选项目未更新?
我有以下 DropDownList 控件:
<asp:DropDownList ID="SubjectFilter" runat="server" AutoPostBack="True" onselectedindexchanged="SubjectFilter_SelectedIndexChanged"></asp:DropDownList>
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"));
一切都加载得很好。但是,在 SubjectFilter_SelectedIndexChanged
方法中,SubjectFilter.SelectedValue
始终为 Default
,即使我选择了不同的选项。
问题是什么? 非常感谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜上面的代码来自
PageLoad
事件。您可能希望将其包装在if(!isPostBack)
块中。I'm guessing the above code is from the
PageLoad
event. You may want to wrap that in aif(!isPostBack)
block.确保在
Page_Load
中仅在IsPostBack
为 false 时填充下拉列表。例如
Make sure that in your
Page_Load
that you only populate the dropdown whenIsPostBack
is false.For example
你什么时候绑定下拉菜单?你可以将任何东西包裹在一个
if(page.ispostback ==false)
在检查其值之前,您可能会在页面加载时进行绑定。
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..
ViewState 在 ASP.NET 页面的 Init 和 Load 之间分配。您的事件处理程序在加载后发生。如果您以编程方式在用户将使用的控件中设置内容,您需要在应用 ViewState 之前处理该内容。换句话说,将其移至Page_Init。之后,ViewState 启动,您将在处理程序执行时看到用户实际选择的内容。
ViewState is assigned between the Init and Load for an ASP.NET page. 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. In other words, move it to Page_Init. Afterwards, ViewState kicks in and you'll see what the user actually selected when your handler executes.