Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework


About Me

Great Blogs

ASP.NET Page lifecycle

In this post i am going to try to give you an overview of the events that take place in 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 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 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.


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)

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

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


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.


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!!!


Damyan said:

The Page_PreInit method should be used to create or re-create dynamic controls, not the Page_Init(it is used to read or initialize control properties).

# September 23, 2010 5:13 AM

Shrek said:

This is a very good overview and as you have mentioned in the article it is very important for developers to understand page lifecycle very well. It will be very usefull especially for biginners if you could publish may be more article on this subject.

# September 23, 2010 7:29 AM

Vaibhav Kumar Sharma said:

Hey this is a really useful article man.....

# September 24, 2010 7:10 AM

Paul said:

this is one of the best blog posts on this subject

# September 25, 2010 11:58 AM

pujitha said:

this is very helpful overview about lifecycle

# February 22, 2013 7:38 AM