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
PreInit
- This event happens with an ASP.NET page request after the start stage is completed and before the initialization stage starts.
- The Ispostback property is determined.
- 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
- The page init event occurs after the raised when controls are initialized.
- This event reads or initializes control properties.
- The server controls are loaded and initialized from the webform view state.
- This event is used to set or get control properties.
Event
protected void Page_Init(object sender, EventArgs e)
{
lblinit.Text += "<br/>" + "Init";
}
InitComplete
- This event is raised immediately after the end of page initialization.
- ViewState is not yet loaded
- 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
- This event happens just before the page load event.
- 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
- 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.
- We can create dynamic controls in this method.
- 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
- This event is raised at the end of the event handling stage.
- 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
- This event is raised just before the rendering stage of the page.
- This event is raised after the page object has created all controls that are required to render the page.
- 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
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
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.