为什么 OnLoad / CreateChildControls 顺序在回发时会发生变化?

发布于 2024-08-08 23:37:40 字数 690 浏览 4 评论 0原文

Web 部件生命周期描述如下:

页面加载

  1. 构造函数
  2. OnInit
  3. 加载时
  4. 如果 Web 部件可连接,则调用 ConnectionConsumer 方法
  5. 创建子控件...

第一次回发时(回发点击 处理程序通过 public 设置 ViewState 属性)

  1. 构造函数
  2. OnInit
  3. 创建子控件
  4. 加载时
  5. 回发点击处理...

在第二次回发时(回发点击 处理程序通过 public 设置 ViewState 属性)

  1. 构造函数
  2. OnInit
  3. 加载视图状态
  4. 创建子控件
  5. 加载...

如您所见,OnLoad 和 CreateChildControls 更改了它们的顺序。这给我的代码带来了一些困难,因为我需要收集我以前在 OnLoad 元素中执行的各种数据。

回发阶段顺序发生变化有什么原因吗?

The web part lifecycle is described like this:

On Page Load

  1. Constructor
  2. OnInit
  3. OnLoad
  4. ConnectionConsumer method is called if web part is connectable
  5. CreateChildControls ...

On 1st Postback (PostBack click
handler sets ViewState via public
Property)

  1. Constructor
  2. OnInit
  3. CreateChildControls
  4. OnLoad
  5. PostBack click handling ...

On 2nd Postback (PostBack click
handler sets ViewState via public
Property)

  1. Constructor
  2. OnInit
  3. LoadViewState
  4. CreateChildControls
  5. OnLoad ...

As you can see the OnLoad and CreateChildControls change their order. This introduces some difficulties in my code as I need to gather various data which I used to do in the OnLoad element.

Is there any reason why the order is changed in the post back phase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

放赐 2024-08-15 23:37:40

每当框架(或您自己)调用 EnsureChildControls 方法时,都会调用 CreateChildControls。每当您需要子控件时就应该调用此方法。

对于框架,它希望在 OnInit 和 OnLoad 之间设置发布的值(以便您可以在 OnLoad 期间访问这些值)。因为它需要控件来执行此操作,所以它会为您调用 EnsureChildControls。

如果没有回发,则也无需设置值,因此对 EnsureChildControls 的调用将等到框架确实需要控件时为止。这恰好发生在 OnLoad 和 OnPreRender 之间。

CreateChildControls is called whenever the framework (or yourself) calls the EnsureChildControls method. This method should be called whenever you need the child controls to be there.

In the case of the framework it wants to set the posted values between the OnInit and the OnLoad (so that you may access the values during OnLoad). Because it needs the controls to do this, it'll call EnsureChildControls for you.

If there is no postback, there is also no need to set the values and thus the call to EnsureChildControls will wait until such a time the framework does need the controls. This happens to be between the OnLoad and the OnPreRender.

逆蝶 2024-08-15 23:37:40

这不应该发生,你绝对确定吗?是否没有其他正在运行的线程可能会使顺序看起来好像已更改?

毕竟,这只是 ASP.NET,一个由计算机执行的程序(这意味着它应该始终做相同的事情,与人不同)。

That shouldn't happen, are you absolutely sure? Are there no other threads running that might make it seem as though the order has changed?

This is after all just ASP.NET, a program executed by a computer (meaning it should always do things the same, unlike a person).

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