Page Life Cycle in ASP.NET

In this article, I will explain the concept of a page Life Cycle in ASP.NET. I have written this article focusing on students and beginners.

Introduction

 
In this article, I will explain the concept of a page Life Cycle in ASP.NET. The Asp.net page life cycle includes the events preinit, Init, InitComplete, OnPreLoad, Load, LoadComplete, OnPreRender, and OnSaveStateComplete. These events take place whenever an ASP.NET page is requested. Events include initialization, instantiating controls, restoring and maintaining state, running event handler code and rendering. I have written this article focusing on students and beginners.
 

Flow Chart

 
Page life Cycle In ASP.NET
 

PreInit

  1. This event happens with an ASP.NET page request after the start stage is completed and before the initialization stage starts.
  2. The Ispostback property is determined.
  3. The event allows us to set the master page and theme of a web application dynamically, create or re-create dynamic controls, and set or get profile property values.
Event
  1. protected void Page_PreInit(object sender, EventArgs e)  
  2.         {  
  3.             lblpreinit.Text += "<br/>" + "PreInit";  
  4.         }  

Init

  1. The page init event occurs after the raised when controls are initialized.
  2. This event read or initialize control properties.
  3. The server controls are loaded and initialized from the webform view state.
  4. This event is used to set or get control properties.
Event
  1. protected void Page_Init(object sender, EventArgs e)  
  2.        {  
  3.            lblinit.Text += "<br/>" + "Init";  
  4.        }   

InitComplete

  1. This event is raised immediately after the end of page initialization.
  2. ViewState is not yet loaded
  3. This event can be used to make changes in ViewState and process initialization tasks to be completed.
Event
  1. protected void Page_InitComplete(object sender, EventArgs e)  
  2.         {  
  3.             lblinitcomplete.Text += "<br/>" + "InitComplete";  
  4.         }  

Preload

  1. This event happens just before the page load event.
  2. It is raised when the page loads the viewstate and viewstate data are loaded to controls.
Event
  1. protected override void OnPreLoad(EventArgs e)  
  2.         {  
  3.             lblpreload.Text += "<br/>" + "PreLoad";  
  4.         }   

Load

  1. The page load event occurs when the page calls the onload method on page object, then a recursive onload for each control is invoked.
  2. We can create dynamic controls in this method.
  3. In this event page lifecycle, all values are restored and then check the value of Isvalid.
Event
  1. protected void Page_Load(object sender, EventArgs e)  
  2.         {  
  3.             lblload.Text += "<br/>" + "Load";  
  4.         }  

LoadComplete

  1. This event is raised at the end of the event handling stage.
  2. Use of this event for tasks that require all other controls on the page be loaded.
Event
  1. protected void Page_LoadComplete(object sender, EventArgs e)  
  2.        {  
  3.            lblloadcomplete.Text += "<br/>" + "LoadComplete";  
  4.        }   

PreRender

  1. This event is raised just before the rendering stage of the page.
  2. This event is raised after the page object has created all controls that are required in order to render the page.
  3. The page object raises the prerender event on the page object, and then recursively does the same for each child control.
Event
  1. protected override void OnPreRender(EventArgs e)  
  2.         {  
  3.            lblprerender.Text += "<br/>" + "PreRender";  
  4.         }   

SaveStateComplete

 
Savestatecomplete event occurs raised after view state and controls state have been saved for the page and for all controls.
 
Event
  1. protected override void OnSaveStateComplete(EventArgs e)  
  2.         {  
  3.             lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";  
  4.         }   
Page life Cycle events
  1. public partial class PagelifeCycle : System.Web.UI.Page  
  2.      {  
  3.   
  4.         protected void Page_PreInit(object sender, EventArgs e)  
  5.         {  
  6.             lblpreinit.Text += "<br/>" + "PreInit";  
  7.         }  
  8.         protected void Page_Init(object sender, EventArgs e)  
  9.         {  
  10.             lblinit.Text += "<br/>" + "Init";  
  11.         }  
  12.         protected void Page_InitComplete(object sender, EventArgs e)  
  13.         {  
  14.             lblinitcomplete.Text += "<br/>" + "InitComplete";  
  15.         }  
  16.          
  17.   
  18.         protected override void OnPreLoad(EventArgs e)  
  19.         {  
  20.             lblpreload.Text += "<br/>" + "PreLoad";  
  21.         }  
  22.          
  23.        protected void Page_Load(object sender, EventArgs e)  
  24.         {  
  25.             lblload.Text += "<br/>" + "Load";  
  26.         }  
  27.         protected void Page_LoadComplete(object sender, EventArgs e)  
  28.         {  
  29.             lblloadcomplete.Text += "<br/>" + "LoadComplete";  
  30.         }  
  31.         protected override void OnPreRender(EventArgs e)  
  32.         {  
  33.             lblprerender.Text += "<br/>" + "PreRender";  
  34.         }  
  35.         protected override void OnSaveStateComplete(EventArgs e)  
  36.         {  
  37.             lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";  
  38.         }  
  39.           
  40.   
  41.          
  42.     }   
Output
 
Page life Cycle In ASP.NET
 

Control Events

 
This event handles specific control events after the page load event. Control events like button click and other dropdownlist events are raised.
 

Flow Chart

 
Page life Cycle In ASP.NET
 
Event
  1. protected void btnSubmit_Click(object sender, EventArgs e)  
  2.         {  
  3.            Response.Write("<br/>" + "btnSubmit_Click");  
  4.         }   
Page Life cycle (Control Events)
  1. public partial class PagelifeCycle : System.Web.UI.Page  
  2.      {  
  3.           
  4.         protected void Page_PreInit(object sender, EventArgs e)  
  5.         {  
  6.             lblpreinit.Text += "<br/>" + "PreInit";         
  7.         }  
  8.         protected void Page_Init(object sender, EventArgs e)  
  9.         {  
  10.             lblinit.Text += "<br/>" + "Init";  
  11.         }  
  12.         protected void Page_InitComplete(object sender, EventArgs e)  
  13.         {  
  14.             lblinitcomplete.Text += "<br/>" + "InitComplete";  
  15.         }  
  16.         protected override void OnPreLoad(EventArgs e)  
  17.         {  
  18.             lblpreload.Text += "<br/>" + "PreLoad";  
  19.         }  
  20.         protected void Page_Load(object sender, EventArgs e)  
  21.         {  
  22.             lblload.Text += "<br/>" + "Load";  
  23.         }  
  24.         protected void btnSubmit_Click(object sender, EventArgs e)  
  25.         {  
  26.             lblsubmit.Text += "<br/>" + "btnSubmit_Click";  
  27.         }  
  28.         protected void Page_LoadComplete(object sender, EventArgs e)  
  29.         {  
  30.             lblloadcomplete.Text += "<br/>" + "LoadComplete";  
  31.         }  
  32.         protected override void OnPreRender(EventArgs e)  
  33.         {  
  34.             lblprerender.Text += "<br/>" + "PreRender";  
  35.         }  
  36.         protected override void OnSaveStateComplete(EventArgs e)  
  37.         {  
  38.             lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";  
  39.         }  
  40.           
  41.   
  42.       }   

Summary

 
In this article, you have been given the concepts of Page Life Cycle in Asp.Net. I have written this article mainly for beginners and students.