简体   繁体   English

Codebehind中的ASP.NET下拉列表与ASPX页面中的相关联

[英]ASP.NET Dropdown List in Codebehind vs in ASPX page

I am generating a dropdown list in codebehind and cannot get the selectedindexchanged event to fire automatically. 我在代码隐藏中生成一个下拉列表,无法自动触发selectedindexchanged事件。 It works fine when put directly into the ASPX page, but I need it to be in the codebehind. 它直接放入ASPX页面时工作正常,但我需要它在代码隐藏中。

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>

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. 您可能在Load事件中执行此操作,为时已晚。 Try adding it during the Init event or overriding the CreateChildControls method. 尝试在Init事件期间添加它或覆盖CreateChildControls方法。

Edit: As Dave Swersky mentioned, make sure you do this on EVERY page request including postbacks. 编辑:正如Dave Swersky所提到的,请确保在每个页面请求(包括回发)上执行此操作。

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): 然后在代码后面(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));
}

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. 详细说明Mike Mooney的答案:您还需要确保在每次回发时将控件添加回控件树 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. 如果以编程方式再次添加它并且从不再添加它,则树中无法控制接收事件。

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

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. 通过添加控件,您不需要直接调用RenderControl。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM