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
- PreInit
protected void Page_PreInit(object sender, EventArgs e)
{
}
- Init
protected void Page_Init(object sender, EventArgs e)
{
}
- InitComplete
protected void Page_InitComplete(object sender, EventArgs e)
{
}
- PreLoad
protected void Page_PreLoad(object sender, EventArgs e)
{
}
- Load
protected void Page_Load(object sender, EventArgs e)
{
}
- Control Events
protected void Button1_Click(object sender, EventArgs e)
{
}
- LoadComplete
protected void Page_LoadComplete(object sender, EventArgs e)
{
}
- PreRender
protected void Page_PreRender(object sender, EventArgs e)
{
}
- PreRenderComplete
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
}
- SaveStateComplete
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
}
- UnLoad
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:
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:
No comments:
Post a Comment