动态下拉列表控件不会触发 Selectedindexchanged 事件
我需要触发动态创建的下拉列表的 Selectedindexchanged 事件。 我在 aspx 页面中没有代码。所有代码都在代码隐藏中。
DropDownList objdropDown = new DropDownList();
objdropDown.EnableViewState = true;
objdropDown.AutoPostBack = true;
//objdropDown.AutoPostBack = true;
//objdropDown.SelectedIndexChanged += new EventHandler(objdropDown_SelectedIndexChanged);
protected void objdropDown_SelectedIndexChanged(object sender, EventArgs e)
{
//My code here
}
从下拉列表中选择随机项目后,它会回发,但下拉控件不可见。我在这里做错了什么?代码会很有帮助。谢谢!
I need to fire the Selectedindexchanged event of a dynamically created dropdownlist.
I've no code in aspx page. All code are in codebehind.
DropDownList objdropDown = new DropDownList();
objdropDown.EnableViewState = true;
objdropDown.AutoPostBack = true;
//objdropDown.AutoPostBack = true;
//objdropDown.SelectedIndexChanged += new EventHandler(objdropDown_SelectedIndexChanged);
protected void objdropDown_SelectedIndexChanged(object sender, EventArgs e)
{
//My code here
}
After selecting a random item drom Dropdownlist, it postbacks but the dropdown control is not visible. What am I doing wrong here? Code would be helpful. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为代码是在后面的代码中生成的,所以它不会在回发之间保留,除非您每次显式重新创建它(可能在 Page_Load 中)。
但是,您可能必须跟踪您选择的项目,并在每次回发重新生成后将其设置为选定的。
Because the code is generated in the code behind it will not be persisted between postbacks, except you explicitly recreate it each time (probably in Page_Load).
But then you might have to track the item you selected and set that as selected after each regeneration on postback.
您需要创建下拉列表&在 pageInit 中尽早绑定事件。然后它将意识到能够参与回发过程并发送消息。引发 selectedindexchange 事件。
PageLoad 已晚,因为回发处理已发生。看看这个: http://msdn.microsoft.com/en-us /library/ms178472.aspx
You need to create the drop-down list & bind the event early, in pageInit. It will be then be aware that be able to participate in the postback process & raise events for selectedindexchange.
PageLoad is to late as postback processing has already occurred. Have a look at this: http://msdn.microsoft.com/en-us/library/ms178472.aspx
您是否将新创建的 DropDownList 添加到其页面上的父容器并将其可见性设置为可见?
Did you add the newly created DropDownList to it's parent Container on the Page and set its Visibility to visible?
我有同样的问题,只是想与其他人分享我的解决方案。请确保
我的问题与回发后更改的动态控件 ID 有关。在确保所有回发中的控件 ID 相同后,我的问题得到解决。
I have the same issue as this and just wanted to share my resolution for others. Please make sure
My issue is relating to the dynamic control ID being changed after post back. My issue is resolved after I make sure that the control ID the same across all post backs.