Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does OnLoad / CreateChildControls order change at postback?

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?

like image 367
spa Avatar asked Oct 30 '09 07:10

spa


1 Answers

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.

like image 119
Robba Avatar answered Sep 20 '22 18:09

Robba