代码隐藏中的 ASP.NET 下拉列表与 ASPX 页面中的 ASP.NET 下拉列表

发布于 2024-08-22 02:18:04 字数 947 浏览 7 评论 0原文

我正在代码隐藏中生成一个下拉列表,但无法自动触发 selectedindexchanged 事件。当直接放入 ASPX 页面时它工作正常,但我需要它位于代码隐藏中。

这不起作用:

var deptList = new DropDownList
    {
        ID = "deptList",
        DataSource = departments,
        DataTextField = "deptname",
        DataValueField = "deptid",
        AutoPostBack = true,
        EnableViewState = true
    };

deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);
deptList.DataSource = departments;
deptList.DataTextField = "deptname";
deptList.DataValueField = "deptid";

if (!IsPostBack)
    deptList.DataBind();

deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));

writer.Write("Select a department: ");
deptList.RenderControl(writer);

但这有效:

<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="deptList_SelectedIndexChanged"></asp:DropDownList>

I am generating a dropdown list in codebehind and cannot get the selectedindexchanged event to fire automatically. It works fine when put directly into the ASPX page, but I need it to be in the codebehind.

This doesn't work:

var deptList = new DropDownList
    {
        ID = "deptList",
        DataSource = departments,
        DataTextField = "deptname",
        DataValueField = "deptid",
        AutoPostBack = true,
        EnableViewState = true
    };

deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);
deptList.DataSource = departments;
deptList.DataTextField = "deptname";
deptList.DataValueField = "deptid";

if (!IsPostBack)
    deptList.DataBind();

deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));

writer.Write("Select a department: ");
deptList.RenderControl(writer);

but this works:

<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="deptList_SelectedIndexChanged"></asp:DropDownList>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

岁月静好 2024-08-29 02:18:04

问题可能在于您没有尽早将控件添加到页面。需要在页面生命周期的早期添加控件以将其事件绑定在一起。

您可能在 Load 事件中执行此操作,但为时已晚。尝试在 Init 事件期间添加它或重写 CreateChildControls 方法。

编辑:正如 Dave Swersky 提到的,确保在每个页面请求(包括回发)上执行此操作。

The problem may be if you are not adding the control to the page early enough. Controls need to be added early in the page lifecycle to get their events tied in.

You're probably doing it in the Load event, which is too late. Try adding it during the Init event or overriding the CreateChildControls method.

Edit: As Dave Swersky mentioned, make sure you do this on EVERY page request including postbacks.

和影子一齐双人舞 2024-08-29 02:18:04

您的代码中有一个网格。尽量将创建、数据绑定和事件调用分开。

示例:

<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server"></asp:DropDownList>

然后在后面的代码中(Page_Load):

deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);

if (!IsPostBack)
{
     deptList.DataTextField = "deptname";
     deptList.DataValueField = "deptid";
     deptList.DataSource = departments;
     deptList.DataBind();
     deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));
}

You have a mesh in your code. Try to devide creating, data binding and events calling.

Example:

<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server"></asp:DropDownList>

Then in code behind (Page_Load):

deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);

if (!IsPostBack)
{
     deptList.DataTextField = "deptname";
     deptList.DataValueField = "deptid";
     deptList.DataSource = departments;
     deptList.DataBind();
     deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));
}
寒尘 2024-08-29 02:18:04

详细说明 Mike Mooney 的答案:您还需要确保在每次回发时将控件添加回控制树。每次回发时都会重新创建控制树,并从标记中读入。如果以编程方式添加一次并且不再添加,则树中没有控件可以接收该事件。

To elaborate on Mike Mooney's answer: you also need to make sure you add the control back into the control tree on every postback. The control tree is recreated on each postback, read in from the markup. If you add it once programmatically and never again, there is no control in the tree to receive the event.

百善笑为先 2024-08-29 02:18:04

您似乎没有将控件添加到控件集合中。您必须将控件添加到控件层次结构中的某个位置,并确保在每次回发时添加该控件,以确保该控件存在以接收事件。通过添加控件,您不需要直接调用 RenderControl。

It appears that you are not adding the control to the controls collection. You must add the control somewhere in the control hierarchy and ensure it gets added on every postback to ensure the control exists to receive the event. By adding the control you shouldn't need to call RenderControl directly.

明月夜 2024-08-29 02:18:04

我遇到的问题是,如果下拉列表没有 AutoPostBack = true 那么它永远不会调用该函数。

The problem i had was that if the drop down list didn't have AutoPostBack = true then it would never call the function.

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