Saturday, February 13, 2010

ASP.NET Page Life Cycle

This is an extract from the page life cycle video available at the asp.net site. You can download the video from the here.

PreInit

We can determine if the request is a post back or an initial rendering early in the life cycle by accessing the IsPostBack property in the PreInit event. If the request is a post back, the values of the controls are still not reconstituted from the view state of the control. We can use this event to set global properties of the page.
Ex: Dynamically set a master page or theme, set profile properties using ASP.NET profile system, create or recreate dynamically created controls (controls that are not added during the design time).

Init

Fired after all the controls on the page have been initialized and any skin settings are applied to those controls. We can use this event to read or write any specific control properties.

InitComplete

Since this event is fired after all the controls on the page are initialized we can use this event to perform tasks that depend on the page and controls to be initialized.

PreLoad

Preload event can be used to do any processing that needs to be before the page and the controls get loaded. After the page preload event all the preload events for the controls on the page are fired. Thereafter all the ViewState data gets initialized followed by postback data from the request object.

Load

After the page load event handler gets called, recursively Unload events for all the controls on the page are called as each control is loaded. After the Load and Unload event handlers are called for all the controls in the page, all the other configured event handlers for the controls are called.
Ex: Click event handlers for buttons, TextChanged event handlers for text boxes, Validation controls events

LoadComplete

We can use the LoadComplete event to perform tasks that require all the controls in the page to be loaded and validated.

PreRender

In this point in time all the controls are created in memory and we need to render those controls, so that the markup can be sent to the browser. Before the PreRender event gets fired, the page object calls to ensure that every child object in the page has its PreRender event called, every data bound control that has a DataSourceID property set has its DataBind method called. We can use the PreRender event to make final changes to the content of the page or the controls of the page before it is turned into html for sending to the browser.

SaveSateComplete

Before this event is fired, ViewState for the entire page and its controls are saved. So any changes done to the state of the page and the controls on the page will not be saved and rendered on the browser. We can use this event to perform any logic against the ViewState but not any modifications. Now the page is ready to be rendered.

There is no Render event but the page object will call all the Render methods of all the controls on the page which will render all the types of controls (built-in controls, user controls, custom controls etc.).

Unload

When Page_Unload event is fired, we cannot do anything with the page because the page is already rendered. If we try to access any page elements during this event, exceptions will be thrown.
Ex: We cannot access the Response object

Unload events will be fired for both the page and each individual control on the page. We can use the Unload event to do final cleanup tasks like closing database connections, closing opened files etc.

1 comment: