What
is Asp.Net Page Life Cycle?
Asp.Net is a powerful platform for building Web
applications. The ASP.NET page life
cycle is a good example to explore so you know how and when page elements are
loaded and corresponding events are fired. When a page is requested, it is
loaded into the server memory, processed and sent to the browser. Then it is
unloaded from the memory. At each of this steps, methods and events are
available, which could be overridden according to the need of the application.
The page
life cycle phases are:
1. Page Initialization (Initialization theme and master
page)
a.
Page_PreInit
b.
Page_Init
c.
Page_InitComplete
2. Page Load (load all control Button,TextBox)
a. Page_PreLoad
b. Page_Load
c.
Page_LoadComplete
3. Page Render (convert server side to client side
(html))
i. Page_Prerender
ii. Page_PreRenderComplete
iii. Page_SaveStateComplete
4. Page Unload (processing destroyed after output)
·
Page_UnLoading
1. Page_PreInit . PreInit is the
first event in page life cycle. means this is the entry point of the ASP.NET
page life cycle - it is the pre-initialization, so you have access to the page
before it is initialized. Controls can be created within this event. Also,
master pages and themes can be accessed. You can check the IsPostBack property
here to determine if it is the first time a page has been loaded.
2. Page_Init . Init event
initializes the control property and the control tree is built. This event can
be handled by overloading the OnInit method or creating a Page_Init handler. This
event fires when all controls on the page have been initialized and skin settings
have been applied.
3. Page_InitComplete . InitComplete event
allows tracking of view state. This event fires once all page and control
initializations complete. This is the last event fired where ViewState is not
set, so ViewState can be manipulated in this event.
4. Page_PreLoad . This event is
triggered before the post back data is loaded in the controls. This event can
be handled by overloading the OnPreLoad method or creating a Page_PreLoad
handler
5. Page_Load . the Load event is
raised for the page first and then recursively for all child controls. The
controls in the control tree are created. This event can be handled by
overloading the OnLoad method or creating a Page_Load handler.
6. Page_LoadComplete . the loading
process is completed, control event handlers are run and page validation takes
place. This event can be handled by overloading the OnLoadComplete method or
creating a Page_LoadComplete handler.
7. Page_PreRender . the PreRender
event occurs just before the output is rendered. By handling this event, pages
and controls can perform any updates before the output is rendered.
8. Page_PreRenderComplete
.
as the PreRender event is recursively fired for all child controls, this event
ensures the completion of the pre-rendering phase.
9. Page_SaveStateComplete
.
state of control on the page is saved. Personalization, control state and view
state information is saved. The HTML markup is generated. This stage can be
handled by overriding the Render method or creating a Page_Render handler.
10. Page_UnLoad . This event fires
for each control and then the page itself. It is fired when the HTML for the
page is fully rendered. This is where you can take care of cleanup tasks, such
as properly closing and disposing database connections..
Example with Page Life Cycle in Asp.net.........................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_PreInit(object sender, EventArgs e)
{
Response.Write("<i><b><font color='red'> 1.
</font></b></i> Page
PreInit <br/>");
}
protected void Page_Init(object sender, EventArgs
e)
{
Response.Write("<i><b><font color='red'> 2.
</font></b></i> Page Init <br/>");
}
protected void
Page_InitComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='red'> 3.
</font></b></i> Page PreInit Complete </br>");
}
protected void
Page_PreLoad(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Green'> 4.
</font></b></i> Page
PreLoad </br>");
}
protected void Page_Load(object sender, EventArgs
e)
{
Response.Write("<i><b><font color='Green'> 5.
</font></b></i>Page Load <br/>");
}
protected void
Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Green'> 6.
</font></b></i> Page Load Complete </br>");
}
protected void
Page_Prerender(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 7.
</font></b></i> Page PreRender</br>");
}
protected void
Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 8.
</font></b></i> Page PreRender Complete</br>");
}
protected void
Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 9.
</font></b></i> Page SaveState Complete</br>");
}
protected void
Button1_Click(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Grey'> 10.
</font></b></i> Button Click </br>");
}
{
Response.Write("<i><b><font color='Pink'> 11.
</font></b></i> Page Unloading Event </br>");
}
}
Nice Article .....
ReplyDeleteit useful article
ReplyDeleteThis blog gives very important info about .Net Thanks for sharing
ReplyDelete.Net Online Training