Thursday, December 16, 2010

Aspx Page Life Cycle

Page Life Cycle can be defined as steps & Events fired between the page request & rendering of page on client side.

Below are the steps in Page Life Cycle:-

P - Page Request

S - Start

I - Initialization

L - Load

V - Validate

E - Event Handling

R - Rendering

U - Unload

D- Dispose



1. Start:- This is where page properties such as Request, Response, IsPostback & UI Culture are set.If we need to access or override behaviour for this step then we can use Preinit() method to create or recreate dynamic controls, set a master page or a theme or read or set profile values. If the request is a Postback then the value of the controls have not yet been restored from view state. If you set a control property at this stage then its value might be overwritten in the next event.

Event:- PreInit()

2. Initialize:- Here themes are applied & unique Ids are generated & set for controls.We can have access to Init(), InitComplete() & Preload() method in this stage.

Event:-
Init():- This event is raised after all controls have been initialized & any skin settings have been applied. We can use this event to read or initialize control properties.

Init Complete():- This event is raised by Page object. We can use this event for processing tasks that require all Initialization be complete.

PreLoad():- This can be use if we need to perform processing on page or control before the Load event.After the page raised this event. It loads viewstate for itself & all controls & then processes any postback data included with Request Instance.

3. Load:- In this stage controls are loaded with information retrieved from view & control state.The Onload() is the event method that fires during this stage.

Event:-
OnLoad():- We can set properties for server controls, Request Query String & establish database connection in this event.

4. Validation:- If we have events that needs validation then they are validate here & we can check there IsValid property.

Event:-
Validate():- It contains one overloaded method that accepts a validation group string.

5. Event Handling:- Controls event handling occurs in this stage.Click, SelectedIndexchanged etc are applied to server controls here & if page is posted back then event handlers fired by the controls.

Event:-
Load Complete:- This event fires when all controls for the page have been loaded.

PreRender:- The page object will call EnsureChildControls for each control & finally for the page. Any data bound control that has a DataSourceId will call its databind method.This event occurs for each control on page.view state will save for all controls & page.

SaveStateComplete:- This event fires after the view state saved for the page & control.

6. Render:- This method is called by page on each control.It writes out the HTML markup for the control to the browser.For our custom controls we can override this method to change the markup of control. If we have override the default asp.net control and we don't want to provide it custom markup then it'll render with the default markup.

7. Unload:- It occurs first for all controls and then for the page.At this stage all controls have been rendered to the output stream & cannot be changed.During this event any attempt to access the response stream will result in an exception.This event is mainly for cleanup routines like closing open database connections, Open file stream, event logging & other tasks.

So basic list of events are as follow:-
1. PreInit()
2. Init()
3. OnInitComplete()
4. Preload()
5. Load()
6. Control Events()
7. LoadComplete()
8. PreRender()
9. PreRenderComplete()
10. Save State Complete()
11. Render()
12. Unload()
13. Dispose()


Thanks & Regards,
Vikas Sharma

No comments:

Post a Comment