Free Web Development Online Tutorials, Learn to Code
Learn Asp.net Aspx, Free Asp.net Tutorial in C#, Asp.net Web Application Development Guide

Asp.Net Page Life Cycle Step by Step

When we say life cycle in Asp.net Application, there are two logical Life Cycle we need to understand.

ASP.Net Application Life Cycle

ASP.Net page Life Cycle

  • Browser send request to IIS
  • ISAPI dll filter all request based on extension (aspx, ascx)
  • HttpRuntime class instantiated and assigned to asp.net worker process.
  • HttpRuntime create a HttpApplication object that pick up application domain from application poll.
  • Page object instance created by HttpApplication and invoke ProcessRequest
  • Initiate the Asp.net Page Life Cycle as given below

ASP.Net Page Life Cycle

In this Article you will learn ASP.Net Page Life Cycle, there are many Events occurs while asp.net pages loading.

On browser when any user request a webpage, it goes through sequence of events to finally generate the response that user see on browser , so during this period some events take place one by one

Page Events in Asp.net

The best way to practice page life cycle in Asp.net , is to create a new page and declare all events on the page, then put break point on each event and run the application, see what happen, in addition if you have some master page and user controls, try to access them and their properties on every event, this exercise will give you the best understanding of each event in page life cycle.

But here are the quick overviews of each event.

Page_PreInit
protected void Page_PreInit(object sender,EventArgs e ) 
{  
         
}
  • Check if loading for first time, IsPostBack
  • Set or re-set dynamic controls like user control or custom control
  • You can change a master page dynamically at this point.
  • Can change theme if required
Any control value can be value overwritten in this event
protected void Page_Init(object sender, EventArgs e ) { }
  • Init event fires after all controls are initialized.
  • The initialization starts with lowest level control first then reach upto Page level
protected void Page_InitComplete(object sender,EventArgs e ) { }
If you need any task to be processed exactly after all initialization is complete, then use this event, At this event viewstate values are loaded.
protected override void OnPreLoad(EventArgs e ) { }
In this event all ViewState data are loaded to controls
protected void Page_Load(object sender,EventArgs e ) { }
This is the event where you need to check if IsPostBack , to avoid unnecessary database call, you also can check IsValid method
protected void Page_LoadComplete(object sender,EventArgs e) { }
In this event you can write methods that may send some tracking information or notification to external services.
protected void Page_PreRender(object sender, EventArgs e) { }
PreRender event occurs just before the all controls are rendered, so if you make any changes in this event that will be saved in ViewState.
protected void Page_LoadComplete(object sender,EventArgs e) { }

Experiment Page Life Cycle
Create a new aspx page with a button an label control on it, notice which events are getting fired at when you load the page first time, then click on button to see what changes has happened and order of page events.

here is the code
public partial class pagelifecycle : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             
            lbPageEvent.Text = lbPageEvent.Text + " " + " Load";
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            
             // Notice: I have set the text to empty at this event, so when you click on submit button on the page, you will realize what all events are getting fired, also try with IsPostBack
            
            lbPageEvent.Text = "";
            lbPageEvent.Text =   "PreInit";
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            
            lbPageEvent.Text = lbPageEvent.Text + " " + "Init";
        }
        protected void Page_InitComplete(object sender, EventArgs e)
        {
            
            lbPageEvent.Text = lbPageEvent.Text + " " + "InitComplete";
        }
        protected override void OnPreLoad(EventArgs e)
        {
            
            lbPageEvent.Text = lbPageEvent.Text + " " + "PreLoad";
        }
        
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
             
            lbPageEvent.Text = lbPageEvent.Text + " " + "btnSubmit_Click";
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
          
            lbPageEvent.Text = lbPageEvent.Text + " " + "LoadComplete";
        }
        protected override void OnPreRender(EventArgs e)
        {
            lbPageEvent.Text = lbPageEvent.Text + " " + "OnPreRender";
        }
        protected override void OnSaveStateComplete(EventArgs e)
        {
                       
            lbPageEvent.Text = lbPageEvent.Text + " " + "OnSaveStateComplete";
        }
        protected void Page_UnLoad(object sender, EventArgs e)
        {
             
            lbPageEvent.Text = lbPageEvent.Text + " " + "PageUnLoad";
        }
         
    }

Comment
Name
Email
Website
Subscribe
 
Asp.net Page Life Cycle with all Events Order