代码隐藏中的 ASP.NET 下拉列表与 ASPX 页面中的 ASP.NET 下拉列表
我正在代码隐藏中生成一个下拉列表,但无法自动触发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题可能在于您没有尽早将控件添加到页面。需要在页面生命周期的早期添加控件以将其事件绑定在一起。
您可能在 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.
您的代码中有一个网格。尽量将创建、数据绑定和事件调用分开。
示例:
然后在后面的代码中(Page_Load):
You have a mesh in your code. Try to devide creating, data binding and events calling.
Example:
Then in code behind (Page_Load):
详细说明 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.
您似乎没有将控件添加到控件集合中。您必须将控件添加到控件层次结构中的某个位置,并确保在每次回发时添加该控件,以确保该控件存在以接收事件。通过添加控件,您不需要直接调用 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.
我遇到的问题是,如果下拉列表没有 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.