Posts Tagged ‘Life Cycle’

Wednesday, November 28th, 2007

ASP.NET Life Cycle

The ASP.NET page life cycle, control or web part life cycle for that matter, is an important concept to know and to understand in ASP.NET web development. ASP.NET, the successor of ASP, introduced the concept of object oriented development. The complete page and its controls are now objects which behave in a child-parent relationship and go through a complete life cycle of their state. It’s pretty crucial to know what stages they go through and which events are called as the web is a stateless medium and we are trying to make a statefull environment out of it.

The key concept is the view state. This is an object, stored within the page, which enholds the states of all current objects. It is loaded, processed and saved during the life cycle. More specifically, it will load all data into the controls right before entering the load event. From this event on, you’ll have controls which are loaded with all previous data. You can manipulate those just until the PreRenderComplete event. Once this event has passed, the viewstate will once again be saved into a hidden control on the page. So things you change during the render event will not be taken into account.

Here is a graphical overview:

ASP.NET Page Life Cycle Overview

Couple of remarks with this diagram:

  • The dark gray events are only available for the page object.
  • The green area indicates events where the viewstate is loaded and avilable in the controls. Once changed the controls will be saved back to the viewstate.

As I said, the viewstate is a pretty important concept to grasp. So, please take time to fully understand it. It’s crucial to know when and where you can interact with it or you’ll be in a whole world of pain when creating interactive web parts or pages. Untill next time, take care!