DropDownList 的 SelectedIndexChanged 事件未触发
我的网页中有一个 DropDownList 对象。当我单击它并选择不同的值时,没有任何反应,即使我有一个函数连接到 SelectedIndexChanged
事件。
首先,实际对象的 HTML 代码:
<asp:DropDownList ID="logList" runat="server"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
这就是该函数 itemSelected
:
protected void itemSelected(object sender, EventArgs e)
{
Response.Write("Getting clicked; " + sender.GetType().ToString());
FileInfo selectedfile;
Response.Write("<script>alert('Hello')</script>");
foreach (FileInfo file in logs)
{
if (file.Name == logList.Items[logList.SelectedIndex].Text)
{
Response.Write("<script>alert('Hello')</script>");
}
}
}
没有任何响应出现,并且 JavaScript 的该部分永远不会运行。我已经在最新的 3.6 版 Firefox 以及 Internet Explorer 8 上尝试过此操作。这是由 Windows Server 2003 R2 计算机提供的,运行 ASP.NET 和 .NET Framework 版本 4。
I have a DropDownList object in my web page. When I click on it and select a different value, nothing happens, even though I have a function wired up to the SelectedIndexChanged
event.
First, the actual object's HTML code:
<asp:DropDownList ID="logList" runat="server"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
And this is that function, itemSelected
:
protected void itemSelected(object sender, EventArgs e)
{
Response.Write("Getting clicked; " + sender.GetType().ToString());
FileInfo selectedfile;
Response.Write("<script>alert('Hello')</script>");
foreach (FileInfo file in logs)
{
if (file.Name == logList.Items[logList.SelectedIndex].Text)
{
Response.Write("<script>alert('Hello')</script>");
}
}
}
None of the Responses appear, and that portion of JavaScript is never run. I've tried this on the latest 3.6 version of Firefox, as well as Internet Explorer 8. This is being served from a Windows Server 2003 R2 machine, running ASP.NET with the .NET Framework version 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将 DropDownList
AutoPostBack
属性设置为 true。例如:
Set DropDownList
AutoPostBack
property to true.Eg:
尝试在 DropDownList 上设置
AutoPostBack="True"
。try setting
AutoPostBack="True"
on the DropDownList.我知道它的帖子有点旧,但我仍然想在上面的答案中添加一些内容。
可能存在某种情况,下拉列表中多个项目的“值”重复/相同。因此,请确保列表项中没有重复的值来触发此“onselectedindexchanged”事件
I know its bit older post, but still i would like to add up something to the answers above.
There might be some situation where in, the "value" of more than one items in the dropdown list is duplicated/same. So, make sure that you have no repeated values in the list items to trigger this "onselectedindexchanged" event
添加属性
ViewStateMode="Enabled"
和EnableViewState="true"
和 DropDownList 中的
AutoPostBack="true"
Add property
ViewStateMode="Enabled"
andEnableViewState="true"
And
AutoPostBack="true"
in drop DropDownList还要确保页面有效。
您可以在浏览器开发人员工具 (F12) 中进行检查。
在控制台选项卡中,选择正确的目标/框架并检查 [Page_IsValid] 属性。
如果页面无效,则表单将不会提交,因此不会触发事件。
Also make sure the page is valid.
You can check this in the browsers developer tools (F12)
In the Console tab select the correct Target/Frame and check for the [Page_IsValid] property
If the page is not valid the form will not submit and therefore not fire the event.
对我来说,答案是 aspx 页面属性,我将 Async="true" 添加到页面属性,这解决了我的问题。
这是我的更新面板的结构
For me answer was aspx page attribute, i added Async="true" to page attributes and this solved my problem.
This is the structure of my update panel
不用自己写,可以直接写在dropdownlist控件的SelectedIndexChanged事件中,例如
Instead of what you have written, you can write it directly in the SelectedIndexChanged event of the dropdownlist control, e.g.
就我而言,问题与组合框值字段中的文本有关,如果文本包含“\r\n”等特殊字符,则不会触发事件 SelectedIndexChanged ,因为一旦编译页面,创建的 javascript 将失败。
In my case the issue was related to the texts in the value filed of the combobox, if the texts contains special characters like "\r\n" the event SelectedIndexChanged will not be fired , because once the page is compiled the created javascript will fail.