Page Life Cycle Of Asp.Net
Asp.net Life Cycle
Introduction:-
Understanding Page lifecycle is very crucial in order to develop ASP.NET applications. Most beginners tend to get
confused while dealing with “dynamic controls” and face problems like losing values, state etc on postbacks.
Since HTTP is stateless, the nature of web programming is inherently different from windows application development,
and the Page lifecycle is one of the primary building blocks while learning ASP.NET.
Event Life Cylcle of an ASP.Net Page
Life cycle stages:-1. Page request
2. Start
3. Page initialization
4. Load
5. Validation
6. Postback event handling
7. Rendering
8. Onload
Life Cycle Events:-
1. PreInit
2. Init
3. InitComplete
4. PreLoad
5. Load
6. Control events
7. LoadComplete
8. PreRender
9. SaveStateComplete
10. Render
11. Unload
1. PreInit()
In this Page Level event, all controls created during Design Time are initialized with their default values.
Syntax :
protected override void OnPreInit(EventArgs e)
{
//custom code
base.OnPreInit(e);
}
Note that PreInit() is the only event where we can set themes programmatically. See the diagram below showing control
hierarchy after the Page_Init() event:
2. OnInit()
In this event, we can read the controls properties (set at design time). We cannot read control values changed by the
user because that changed value will get loaded after LoadPostData() event fires. But we can access control values
from the forms POST data as:
1 string selectedValue = Request.Form[controlID].ToString();
Syntax:
protected override void OnInit(EventArgs e)
{
//custom code
base.OnInit(e);
}
3. PreLoad
Use this event if you need to perform processing on your page or control before the Load event.
After the Page raises this event, it loads View State for itself and all controls, and then processes any postback
data included with the Request instance.
The PreLoad event is raised after all postback data processing and before the Load event.
protected override void OnPreLoad(EventArgs e)
{
//custom code
base.OnPreLoad(e);
}
4. Load
This method notifies the Server Control that it should perform actions common to each HTTP request for the page
it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the
hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.
Use the IsPostBack property to determine whether the page is being loaded in response to a client postback, or if it is
being loaded and accessed for the first time.
Control Event Handlers
syntax:
protected void Page_Load(object sender, EventArgs e)
{
//code here
}
5. Control Event Handlers
These are basically event handelers (like Button1_Click()) which are defined for controls in the ASPX markup. Another
source of confusion arises when the developer thinks that an event handler like Button_Click() should fire independently
(like in windows apps) as soon as he clicks a Button on the web form, forgetting that Page_Load will fire first before any
event handlers.
syntax:
void GreetingBtn_Click(Object sender, EventArgs e)
{
//code
}
6.OnLoadComplete
The LoadComplete event occurs after all postback data and view-state data is loaded into the page and all controls on
the page. In order for view state to work for controls that are added dynamically, they must be added in or before the
pre-render stage of the page life cycle.
syntax:
protected override void OnLoadComplete(EventArgs e)
{
//custom code
base.OnLoadComplete(e);
}
7. PreRender
This event is again recursively fired for each child controls in the Page. If we want to make any changes to control
values, this is the last event we have to peform the same.
protected override void OnPreRender(EventArgs e)
{
//custom code
base.OnPreRender(e);
}
8.SaveStateComplete
Before this event occurs, ViewState has been saved for the page and for all controls. Any changes to the page or controls
at this point will be ignored.
Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
protected override void OnSaveStateComplete(EventArgs e)
{
//custome code
base.OnSaveStateComplete(e);
}
9.Render
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET
web server controls have a Render method that writes out the control’s markup that is sent to the browser.
Syntax:
protected override void Render(HtmlTextWriter writer)
{
//custom code
base.Render(writer);
}
10.Unload
This event occurs for each control and then for the page. In controls, use this event to do final cleanup for specific
controls, such as closing control-specific database connections.
protected override void OnUnload(EventArgs e)
{
//custom code
base.OnUnload(e);
}
No comments:
Post a Comment