Page Life Cycle in ASP.NET

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

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

protected void Page_PreInit(object sender, EventArgs e)  
{  
    lblpreinit.Text += "<br/>" + "PreInit";  
}  

Init

  1. The page init event occurs after the raised when controls are initialized.
  2. This event reads or initializes 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

protected void Page_Init(object sender, EventArgs e)   
{   
    lblinit.Text += "<br/>" + "Init";   
}   

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

protected void Page_InitComplete(object sender, EventArgs e)   
{   
    lblinitcomplete.Text += "<br/>" + "InitComplete";   
}   

Preload

  1. This event happens just before the page load event.
  2. It is raised when the page loads the ViewState and view state data are loaded to controls.

Event

protected override void OnPreLoad(EventArgs e)   
{   
    lblpreload.Text += "<br/>" + "PreLoad";   
}   

Load

  1. The page load event occurs when the page calls the onload method on the page object, and 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 the value of Isvalid.

Event

protected void Page_Load(object sender, EventArgs e)   
{   
    lblload.Text += "<br/>" + "Load";   
}   

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 to be loaded.

Event

protected void Page_LoadComplete(object sender, EventArgs e)   
{   
    lblloadcomplete.Text += "<br/>" + "LoadComplete";   
}   

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

protected override void OnPreRender(EventArgs e)   
{   
    lblprerender.Text += "<br/>" + "PreRender";   
}   

SaveStateComplete

The savestatecomplete event occurs after the view state and controls state have been saved for the page and all controls.

Event

protected override void OnSaveStateComplete(EventArgs e)   
{   
    lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";   
}   

Page life cycle events

public partial class PagelifeCycle : System.Web.UI.Page   
{   
    protected void Page_PreInit(object sender, EventArgs e)   
    {   
        lblpreinit.Text += "<br/>" + "PreInit";   
    }   
    protected void Page_Init(object sender, EventArgs e)   
    {   
        lblinit.Text += "<br/>" + "Init";   
    }   
    protected void Page_InitComplete(object sender, EventArgs e)   
    {   
        lblinitcomplete.Text += "<br/>" + "InitComplete";   
    }      
    protected override void OnPreLoad(EventArgs e)   
    {   
        lblpreload.Text += "<br/>" + "PreLoad";   
    }      
    protected void Page_Load(object sender, EventArgs e)   
    {   
        lblload.Text += "<br/>" + "Load";   
    }   
    protected void Page_LoadComplete(object sender, EventArgs e)   
    {   
        lblloadcomplete.Text += "<br/>" + "LoadComplete";   
    }   
    protected override void OnPreRender(EventArgs e)   
    {   
        lblprerender.Text += "<br/>" + "PreRender";   
    }   
    protected override void OnSaveStateComplete(EventArgs e)   
    {   
        lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";   
    }   
}   

Output

PreInt

Control events

This event handles specific control events after the page load event. Control events like button clicks and other dropdown list events are raised.

Flow chart

Flow Chart

Event

protected void btnSubmit_Click(object sender, EventArgs e)   
{   
    Response.Write("<br/>" + "btnSubmit_Click");   
}   

Page life cycle (control events)

public partial class PagelifeCycle : System.Web.UI.Page   
{   
    protected void Page_PreInit(object sender, EventArgs e)   
    {   
        lblpreinit.Text += "<br/>" + "PreInit";           
    }   
    protected void Page_Init(object sender, EventArgs e)   
    {   
        lblinit.Text += "<br/>" + "Init";   
    }   
    protected void Page_InitComplete(object sender, EventArgs e)   
    {   
        lblinitcomplete.Text += "<br/>" + "InitComplete";   
    }   
    protected override void OnPreLoad(EventArgs e)   
    {   
        lblpreload.Text += "<br/>" + "PreLoad";   
    }   
    protected void Page_Load(object sender, EventArgs e)   
    {   
        lblload.Text += "<br/>" + "Load";   
    }   
    protected void btnSubmit_Click(object sender, EventArgs e)   
    {   
        lblsubmit.Text += "<br/>" + "btnSubmit_Click";   
    }   
    protected void Page_LoadComplete(object sender, EventArgs e)   
    {   
        lblloadcomplete.Text += "<br/>" + "LoadComplete";   
    }   
    protected override void OnPreRender(EventArgs e)   
    {   
        lblprerender.Text += "<br/>" + "PreRender";   
    }   
    protected override void OnSaveStateComplete(EventArgs e)   
    {   
        lblsavestatecomplete.Text += "<br/>" + "SaveStateComplete";   
    }   
}   

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.


Similar Articles