动态下拉列表控件不会触发 Selectedindexchanged 事件

发布于 2024-12-05 11:14:08 字数 533 浏览 4 评论 0原文

我需要触发动态创建的下拉列表的 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 技术交流群。

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

发布评论

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

评论(4

笑梦风尘 2024-12-12 11:14:08

因为代码是在后面的代码中生成的,所以它不会在回发之间保留,除非您每次显式重新创建它(可能在 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.

电影里的梦 2024-12-12 11:14:08

您需要创建下拉列表&在 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

爱本泡沫多脆弱 2024-12-12 11:14:08

您是否将新创建的 DropDownList 添加到其页面上的父容器并将其可见性设置为可见?

Did you add the newly created DropDownList to it's parent Container on the Page and set its Visibility to visible?

暮光沉寂 2024-12-12 11:14:08

我有同样的问题,只是想与其他人分享我的解决方案。请确保

  • 下拉控件 ID 在所有回发中都相同(最好将其设置为安全起见)
  • 在 page_Init 事件中创建动态下拉列表。

我的问题与回发后更改的动态控件 ID 有关。在确保所有回发中的控件 ID 相同后,我的问题得到解决。

I have the same issue as this and just wanted to share my resolution for others. Please make sure

  • The drop down control ID is the same across all the post backs (better set this to be on the safe side)
  • Create the dynamic drop down in the page_Init event.

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.

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