ASP.Net page life cycle

Here is I’m going to explain, behind the scenes when a ASP.NET page loads.

In general, the life cycle of a ASP.NET page comprises of a series of steps like initialization, instantiation of controls, state management, the firing of event handler code, and the final stage of rendering.

The following are the various stages a page goes through:

Page Request

The request page is either parsed or compiled, or fetched from the cache.

Start

Page properties like the Request and Response are set. The type of request is determined, specifically whether it is a new Request or it is a PostBack. Culturing properties are also determined via the pages ICulture property.

Page Initialization

All the controls on the given page are initialized with a UniqueID (please don’t confused this with the ID property as the UnqiueID is a unique, hierarchicial identifier which includes the server control’s naming container). Theming is also applied at this stage.

Load

If the current request is a PostBack, the data from the viewstate and control state is loaded to the appropriate properties of the controls.

Validation

The Validate method of all the validator controls are fired, which in turn sets the Boolean property IsValid of the controls.

Postback Event Handling

If the current request is a PostBack, all event handlers are called.

Rendering

ViewState data is saved for all the controls that have enabled viewstate. The Render method for all the controls is fired which writes its output to the OutputStream via a text writer.

Unload

Once the page has been rendered and sent, the Page’s properties are unloaded (cleanup time).

So know you have a better understanding of the various stages of a ASP.NET pages life cycle, you should be aware that within each of the above stages there are events that you can hook into so that your code is fired at the exact time that you want it to.

Event Wire-up is another important concept to understand. So Event wire-up is where ASP.NET looks for methods that match a naming convention (e.g. Page_Load, Page_Init, etc) , and these methods are automatically fired at the appropriate event. There is a page level attribute AutoEventWireup that can be set to either true or false to enable this behaviour.

Below are some of the more popular events that you should understand as you will most likely be interested in them:

PreInit

You can use this event to:

· Create /recreate dynamic controls

· Set a master page or theme dynamically

· Access profile properties

Init

Used to read or initialize your control properties.

InitComplete

Used when you need to access your properties after they have been initialized.

PreLoad

Used when you need to process things before the Load event has been fired.

Load

The Page’s OnLoad method and all of its child control’s OnLoad method are fired recursively. This event is used to set properties and make database connections

Control Events

Control specific events are handled at this stage. e.g. Click event’s for the button control.

LoadComplete

This stage is for when you need to access controls that have been properly loaded.

PreRender

Can be used to make any ‘last chance’ changes to the controls before they are rendered.

SaveStateComplete

This event is used for things that require view state to be saved, yet not making any changes to the controls themselves.

Render

The page object calls this method on all of the controls, so all the controls are written and sent to the browser.

Unload

All the controls UnLoad method are fired, followed by the pages UnLoad event (bottom-up). This stage is used for closing database connections, closing open files, etc.

It is import to understand that each server control has its very own life cycle, but they are fired recursively so things may not occur at the time you think they do (they occur in reverse order!). What this means is that some events fire from the bottom up like the Init event, while others load from the top-down like the Load event.


...S.VinothkumaR.


1 comment:

Ajit said...

Hi,
This is one of the best description I have came across related to Page Life Cycle.

Although the same contenet are there on internet but this one is well organised.

Thanks a lot.
Ajit Kumar
Thomson