DropDownList 的 SelectedIndexChanged 事件未触发

发布于 2024-10-16 08:46:23 字数 946 浏览 3 评论 0原文

我的网页中有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

泪冰清 2024-10-23 08:46:23

将 DropDownList AutoPostBack 属性设置为 true。

例如:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>

Set DropDownList AutoPostBack property to true.

Eg:

<asp:DropDownList ID="logList" runat="server" AutoPostBack="True" 
        onselectedindexchanged="itemSelected">
    </asp:DropDownList>
葵雨 2024-10-23 08:46:23

尝试在 DropDownList 上设置 AutoPostBack="True"

try setting AutoPostBack="True" on the DropDownList.

埋葬我深情 2024-10-23 08:46:23

我知道它的帖子有点旧,但我仍然想在上面的答案中添加一些内容。

可能存在某种情况,下拉列表中多个项目的“”重复/相同。因此,请确保列表项中没有重复的值来触发此“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

刘备忘录 2024-10-23 08:46:23

添加属性 ViewStateMode="Enabled"EnableViewState="true"
和 DropDownList 中的 AutoPostBack="true"

Add property ViewStateMode="Enabled" and EnableViewState="true"
And AutoPostBack="true" in drop DropDownList

暮光沉寂 2024-10-23 08:46:23

还要确保页面有效。
您可以在浏览器开发人员工具 (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.

无力看清 2024-10-23 08:46:23

对我来说,答案是 aspx 页面属性,我将 Async="true" 添加到页面属性,这解决了我的问题。

<%@ Page Language="C#" MasterPageFile="~/MasterPage/Reports.Master"..... 
    AutoEventWireup="true" Async="true" %>

这是我的更新面板的结构

<div>
  <asp:UpdatePanel ID="updt" runat="server">
    <ContentTemplate>

      <asp:DropDownList ID="id" runat="server" AutoPostBack="true"        onselectedindexchanged="your server side function" />

   </ContentTemplate>
  </asp:UpdatePanel>
</div>

For me answer was aspx page attribute, i added Async="true" to page attributes and this solved my problem.

<%@ Page Language="C#" MasterPageFile="~/MasterPage/Reports.Master"..... 
    AutoEventWireup="true" Async="true" %>

This is the structure of my update panel

<div>
  <asp:UpdatePanel ID="updt" runat="server">
    <ContentTemplate>

      <asp:DropDownList ID="id" runat="server" AutoPostBack="true"        onselectedindexchanged="your server side function" />

   </ContentTemplate>
  </asp:UpdatePanel>
</div>
冰魂雪魄 2024-10-23 08:46:23

不用自己写,可以直接写在dropdownlist控件的SelectedIndexChanged事件中,例如

protected void ddlleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
 //code goes here
}

Instead of what you have written, you can write it directly in the SelectedIndexChanged event of the dropdownlist control, e.g.

protected void ddlleavetype_SelectedIndexChanged(object sender, EventArgs e)
{
 //code goes here
}
孤独难免 2024-10-23 08:46:23

就我而言,问题与组合框值字段中的文本有关,如果文本包含“\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文