ASP.NET 预加载回发事件
是否有任何类型的事件允许预加载回发事件。
我问的原因是我有一个控件,可以在回发事件上向其添加同级控件,但是,当它加载回发时,为时已晚,无法将新控件添加到控件集合中。 因此,控件永远不会正确更新。
谢谢!
Is there any kind of event out there that would allow for a preload post back event.
The reason I ask is I have a control that adds sibling controls to it on postback events, however, by the time it has loaded the post back its too late to add the new control to the control collection. Therefore, the controls are never updated correctly.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试 Init 事件。
Try the Init event.
覆盖
CreateChildControls
(确保调用base!)。 在回发事件处理程序中,确保在某处存储应动态创建的控件列表,因此当在下一轮生命周期的早期调用 CreateChildControls 时,它将重新创建控件建立在最后的回发之上。Override
CreateChildControls
(make sure to call base!). In your postback event handler, make sure you are storing somewhere the list of controls that should be created dynamically, so whenCreateChildControls
gets invoked very early in the lifecycle on the next go-round, it will recreate the controls built on the last postback.这是一个快速技巧。 您始终可以在 init 中查询 __EventTarget 和/或提交按钮的值,并可以动态加载控件。
但这样做可能不合适,因为您的控制层次结构会发生变化并可能导致问题。
Here is a quick hack. You can always, query the __EventTarget and or the value of the submit button in init and can load dynamically the control.
But doing so, may not be appropriate as your control hierarchy would change and could cause problems.
如上所述,必须在页面 Init 事件期间添加动态控件,以便可以在页面的 Viewstate 中正确处理它们。 您可能还想关闭页面的视图状态,因为如果控件发生更改,它可能会向您引发错误。
As above, dynamic controls have to be added during the page Init event, so that they can be properly handled within the page's Viewstate. You might want to turn the Viewstate off for the page as well, since it can fire errors at you if the controls change.
正如已经指出的,添加动态控件的正确位置是在 Init 事件中。
这是一篇包含更多信息的文章。
动态 Web 控件、回发、和视图状态
要更好地了解 ASP .NET 页面生命周期,请参阅:
ASP.NET 页面生命周期概述
As has already been stated the proper place to add dynamic controls is in the Init event.
Here's an article with more information.
Dynamic Web Controls, Postbacks, and View State
To get a better understanding of the ASP .NET page life cycle see:
ASP.NET Page Life Cycle Overview
此页面解释了事件顺序(以及每个事件中发生的情况)在回发中,它不止一次地帮助了我。
我刚刚找到此链接< /a>,这对你也有用
This page explains the event order (and what happens in each one) in a postback, it helped me more than once.
I've just found this link, that can also be of use to you