ASP.NET Page Life Cycle Events
In previous post i told you about Button_Click() event. This event was called when we click a button. In the similar way, ASP.NET page has life cycle events and methods which are called when the page is executed. When a page is requested by the user it will first load it into the server memory and then process it and displays it into the browser. When we close the browser it unloads the content from the memory. All these steps events are called to perform each take. This happens automatically. We can even override these events for our application.
Page Life Cycle Events
It is the first event in the life cycle. It checks the IsPostBack property i.e the page is loaded for the first time or not. This event also sets the master page if it is used in the application(Details of Master page will be covered later).
protected void Page_PreInit(
object sender,
EventArgs e)
{
}
This event initialize all the controls and its control properties.
protected void Page_Init(
object sender,
EventArgs e)
{
}
It is raised when the initialization is complete. Here viewstate variables are loaded.
protected void Page_InitComplete(
object sender,
EventArgs e)
{
}
In this event postback data and viewstate are loaded.
protected void Page_PreLoad(
object sender,
EventArgs e)
{
}
In this event the page is loaded. We can add code in this event if we want something to be done when the page is loaded. For example, if we want to check the IsPostback() property we can write it here.
protected void Page_Load(
object sender,
EventArgs e)
{
}
This event handles the postback events like button click
, TextBox control's TextChanged events.
protected void Button1_Click(
object sender,
EventArgs e)
{
}
This event show the end of page loading.
protected void Page_LoadComplete(
object sender,
EventArgs e)
{
}
This event is called just before the output in rendered.
protected void Page_PreRender(
object sender,
EventArgs e)
{
}
It ensures the completion of PreRender event.
protected void Page_PreRenderComplete(
object sender,
EventArgs e)
{
}
All control's state are saved. Viewstate information is also saved.
protected void Page_SaveStateComplete(
object sender,
EventArgs e)
{
}
This event is the last event called to unload all the controls recursively and at last the page itself.
protected void Page_UnLoad(object sender, EventArgs e)
{
}
Now let us code these things.
Create a page having one label and a button for button click event.
Design
CodeBehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace page_life_cycle_event
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "PreInit";
}
protected void Page_Init(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "Init";
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "InitComplete";
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "PreLoad";
}
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "Load";
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "Button Click";
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "LoadComplete";
}
protected void Page_PreRender(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "PreRender";
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "PreRenderComplete";
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "SaveStateComplete";
}
protected void Page_UnLoad(object sender, EventArgs e)
{
Label1.Text = Label1.Text + "</br>" + "Unload";
}
}
}
Now just run the application.
When page loads and the output will be: