[英]DropDownList doesn't postback on SelectedIndexChanged
I'm writing an ASP.Net webform with some DropDownList controls on it.我正在编写一个带有一些 DropDownList 控件的 ASP.Net 网络表单。 Then user changes selected item in one of dropdowns, ASP.Net doesn't seem to handle SelectedIndexChanged event until form is submitted with a 'Submit' button click.
然后用户更改下拉列表之一中的选定项目,ASP.Net 似乎没有处理 SelectedIndexChanged 事件,直到通过单击“提交”按钮提交表单。 How do I make my dropdowns handle SelectedIndexChanged instantly?
如何让我的下拉列表立即处理 SelectedIndexChanged?
PS It's a classic question I have answered too many times, but it seems no one asked it before on stackoverflow. PS 这是一个经典问题,我已经回答了太多次了,但似乎没有人在 stackoverflow 上问过这个问题。
Setting the AutoPostback property to true will cause it to postback when the selection is changed.将AutoPostback属性设置为 true 将导致它在选择更改时回发。 Please note that this requires javascript to be enabled.
请注意,这需要启用 javascript。
You need to set the AutoPostBack
property of the list to true.您需要将列表的
AutoPostBack
属性设置为 true。
Also, if you're populating the contents of the drop down list from the code behind (getting the contents of the list from a database, for example) - make sure you're not re-binding the data in every postback.此外,如果您要从后面的代码中填充下拉列表的内容(例如,从数据库中获取列表的内容),请确保您没有在每次回发中重新绑定数据。
Sometimes people are caught out by binding the drop-down in the page load event without putting it in an If Not IsPostBack
.有时人们会因为在页面加载事件中绑定下拉菜单而不将其放入
If Not IsPostBack
中而被抓住。 This will cause the event not to fire.这将导致事件不触发。
The same is also true of repeaters and ItemCommand
events.中继器和
ItemCommand
事件也是如此。
if you are populating the dropdown list during page load then each time the page postback it will reload the list thus negating your postback method.如果您在页面加载期间填充下拉列表,那么每次页面回发时它都会重新加载列表,从而否定您的回发方法。 you need to be sure to load the dropdownlist only if (!ispostback)
您需要确保仅在 (!ispostback) 时加载下拉列表
Set the AutoPostBack property of DropDownList to true.将 DropDownList 的 AutoPostBack 属性设置为 true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.