Sunday 22 November 2015

Life Cycle and Events of Asp.Net Page

Introduction:

I will explain here about life cycle and what are the events occur in the life cycle of page.

Explanation:

 Life Cycle of Page

Web page request comes from browser.

•    IIS maps the ASP.NET file extensions to ASPNET_ISAPI.DLL, an ISAPI extension provided with ASP.NET.

•    ASPNET_ISAPI.DLL forwards the request to the ASP.NET worker process (ASPNET_WP.EXE or W3P.EXE).

•    ISAPI loads HTTPRuntime and passes the request to it. Thus, HTTP Pipelining has begun.

•    HTTPRuntime uses HttpApplicationFactory to either create or reuse the HTTPApplication object.


•    HTTPRuntime creates HTTPContext for the current request. HTTPContext internally maintains HTTPRequest and HTTPResponse.

•    HTTPRuntime also maps the HTTPContext to the HTTPApplication which handles the application level events.

•    HTTPApplication runs the HTTPModules for the page requests.

•    HTTPApplication creates HTTPHandler for the page request. This is the last stage of HTTPipelining.

•    HTTPHandlers are responsible to process request and generate corresponding response messages.

•    Once the request leaves the HTTPPipeline, page level events begin.

•    Page Events are as follows: PreInit, Init, InitComplete, PreLoad, Load, Control events (Postback events), Load Complete, PreRender, SaveStateComplete, Render and Unload.

•    HTTPHandler generates the response with the above events and sends back to the IIS which in turn sends the response to the client browser.

Events in the Life Cycle of Page

PreInit: All the Pre and Post events are introduced as part of .NET Framework 2.0. As the name suggests, this event is fired before the Init method is fired. Most common functionalities implemented in this method include:

•    Check the IsPostBack property
•    Set the master page dynamically
•    Set the theme property of the page dynamically
•    Read or Set the profile property values
•    Re-create the dynamic controls

Init: This event is raised after all controls in the page are initialized and any skin settings have been applied. This event is used to read or initialize control properties. It can be used to register events for some controls for which the events are not specified in the aspx page.
Ex: OnClick event of the Button can be registered in the Init rather than specifying in the OnClick property of the Button in the aspx page.

InitComplete: Use this event for processing tasks that require all initialization to be complete.

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.

Load: The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded. Use the OnLoad event method to set properties in controls and establish database connections.
Control events: Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.

LoadComplete: Use this event for tasks that require that all other controls on the page be loaded.

PreRender: This is the last event raised before the HTML code is generated for the page. The PreRender event also occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.
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 to perform tasks that require view state to be saved, but that do not make any changes to controls.

Render: This is the stage where the HTML code for the page is rendered. The Page object calls the Render method of each control at this stage. All ASP.NET Web server controls have a Render method that writes out the control's markup that is sent to the browser.

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.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks. .

No comments:

Post a Comment