ASP.NET Page lifecycle

In this post i am going to try to give you an overview of the events that take place in asp.net lifecycle. Page Life Cycle in ASP.NET is essential knowledge for developing applications.

When a web page is sent to the Web Server for processing, it goes through a sequence of steps before it finally gets displayed in the Web Browser.

Without understanding the Page Life Cycle, developing asp.net apps will be more difficult than it should be.

In order to backup my previous statement I will present you a problem a friend of mine was facing.

I got an email the other day from a friend saying,

 "I want to change themes in an asp.net application depending on user's choice. I have set a cookie and read user's preferences. I have the code implemented but it doesn't work."

His code was more or less like this:

 
 protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.Cookies["theme"] != null)
        {
            this.Theme = Request.Cookies["theme"].Value;
        }
    } 

 Of course the above code would never work. The problem lies in the Page events lifecyle.

He should handle the PreInit event. This event occurs before a page is initialized.

You can only change the theme of a page during this event. Once a page becomes initialized, its theme is set and cannot be changed.

He should not type his code in the Load event. This event occurs after a page is initialized and once a page becomes initialised, its theme is set and cannot be changed. 


There are 3 phases in page construction also known as page lifecycle. These are:

  • Initialisation
  • Event Handler Execution
  • Rendering

 

We are going to look into those 3 major phases. We must keep in mind that page lifecycle is just a part of the entire Application lifecycle.

Initialisation


In this phase the objects that represent the page controls are instantiated and set to their initial properties.

3 main events happen in this phase:

PreInit(In this event we can write code to set dynamically a theme or a master page)
Init
InitComplete

You can use the Page_Init event to create or re-create the controls that need to be created or re-created dynamically.


Event Handler Execution


Afer everything is being lodaded in its inital state, the control’s specific events fire. The order of events is this

Preload
Page_Load (We write lots of initialisation code in this event)
Controls events (Changed events, PostBackEvent and validation events are fired)
Load Complete

Page_Load

At this phase the OnLoad event method on the Page is called and then the same applies for each child control, which does the same for each of its child controls until the page and all controls are loaded.

Rendering


In this phase the control tree is translated into HTML. Keep in mind that our browser understands HTML only…. The main events in this phase are:

PreRender - Use the Pre_Render event to make any last minute changes to the contents of the web page or any of its controls.
SaveState - happens after the ViewState for the page has been created and set
Render - Each control calls its Render method and outputs HTML
Unload - After the markup is created for the whole page and sent back to the http application each object that was instantiated is unloaded and is available for garbage collection


You cannot remember all these events.But having a clue about the basic course of events helps a lot when you have debugging issues and have architectural dilemmas about your application.



Hope it helps!!!

5 Comments

Comments have been disabled for this content.