简体   繁体   English

所选项目未更新?

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

相关问题 为什么我的组合框项目没有被发现和选择? - Why is my combobox item not being discovered and selected? 在页面加载时在ListView中选择项目 - Item is being selected in ListView on page load [C#][WPF] 修改datagrid的选中项后不再更新 - [C#][WPF] After modifying the selected item of datagrid, it is no longer updated Nhibernate-项目将其ParentID更新为null,而不是被删除 - Nhibernate - item gets its ParentID updated to null instead of being deleted 如何获得所选项目的索引号? - How do you get the index number of the item being selected? WPF 组合框初始值被所选项目覆盖 - WPF Combobox initial value being overridden by selected item ASP.NET 核心 razor 中的选定项目未通过 - Selected item in ASP.NET Core razor not being passed 绑定期间未创建未选择第一项的列表 - List without first item selected not being created during binding ListBox和Datasource - 阻止选择第一个项目 - ListBox and Datasource - prevent first item from being selected 当我们从gridview选中的行中有相应的datatextfield时,如何显示下拉列表中的项目被选中 - How to show item in dropdownlist as being selected when we have corresponding datatextfield from gridview selected row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM