Sign In
The Sug > Blogs > // todo: be awesome > Posts > WebPart Life Cycle
November 04
WebPart Life Cycle
Because I consistently forget and have to look it up, I decided to post the life cycle of an ASP.NET WebPart.

On page load:
  1. OnInit
  2. OnLoad
  3. Connection Consuming
  4. CreateChildControls
  5. OnPreRender
  6. Render

On post back:

  1. OnInit
  2. CreateChildControls
  3. OnLoad
  4. Page Control Event Handling (button clicks, drop down index changes, etc.)
  5. Connection Consuming
  6. OnPreRender
  7. Render

Note how the connection code runs before CreateChildControls on load and after it on post back.  If you write code where CreateChildControls depends on a value that is set from a connection, you will need to do something creative to handle it. 

You could, for example, clears the controls, and recreate them:



this.Controls.Clear();
this.ChildControlsCreated = false;
this.EnsureChildControls()


Comments

There are no comments for this post.