.net 页面事件触发顺序更改
好吧,这是一个非常烦人的错误,我整个早上都遇到了问题!
我有一个自定义控件,我们已在许多项目中使用该控件,该控件具有通过调用页面 onload 设置并存储在 Viewstate 中的属性。 该控件使用自定义控件的 CreateChildControls() 方法上的属性设置子控件。
通常,与往常一样,在回发时会触发 Page_Load 事件,然后触发页面上控件的 CreateChildControls 方法。
但奇怪的是,我们在网站上有一个登录系统(自定义会员提供程序),当用户登录时,首先会发生相反的情况,CreateChildControls() 方法会触发,然后是 Page_Load,因此控件属性是错误的(从之前的回发设置) )
事件如何以不同的顺序触发? 我认为无论如何,所有页面事件都以相同的顺序发生,并且我不知道登录会如何改变该顺序。
更新:问题似乎是我没有调用 EnsureChildControls() 但我不确定应该在哪里调用它? 如果在控件上设置了用于设置子控件的多个属性,那么我什么时候应该调用 EnsureChildControls(),我想我不完全理解 EnsureChildControls() 的作用?
Ok this is a really annoying bug that I have been having issues with all morning!.
I have a custom control that we have used on many project that has properties that are set and stored in Viewstate by the calling pages onload. the control sets up childcontrols with propertes on the CreateChildControls() method of the custom control.
Normally as usual on a postback the Page_Load event is fired then the CreateChildControls method of the control on the page is fired.
The strange thin though is we have a login system (custom membership provider) on the site and when a user is logged in the opposite happens first the CreateChildControls() method fires then the Page_Load so the control properties are wrong (set from the previous postback)
How could the events be firing in a different order? I thought all page events happened in the same order no matter what and I don't see how being logged in would change that order.
UPDATE: It seems the issue is I'm not calling EnsureChildControls() but I'm not sure where it should be called? If several properies are set on the control which are used in setting up the child controls when should I call EnsureChildControls(), I guess I don't fully understand what EnsureChildControls() does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要 ASP.NET 页面需要 CreateChildControls,就会调用它们。 页面周期中没有具体的点。 它可以发生在Init事件中,也可以发生在Load事件中。 如果要确保子控件可用,请调用控件的 EnsureChildControls() 方法。 您可以在控件的 Init 事件中执行此操作,以确保在整个生命周期中拥有子控件,或者在需要引用其中一个子控件时执行此操作 - 例如,在控件属性的 getter/setter 中。
CreateChildControls is called whenever the ASP.NET page needs them. There is no specific point in the page cycle for that. It can happen in the Init event, it can happen in the Load event. If you want to make sure your child controls are available, then call EnsureChildControls() method of your control. You can do that in the control's Init event to make sure you have child controls through the whole lifecycle or you can do it whenever you need a reference to one of the child controls - e.g. in the getter/setter of a property of your control.
当创建需要访问所包含的子控件的服务器/用户控件的属性时,我使用以下内容:
这可确保您的控件使用者可以在页面生命周期的各个阶段自由地使用您的控件。
When creating properties of a server/user control that need access to contained child controls I use the following:
This ensures your control consumers are free to work with your control at various stages of the page lifecycle.