Learning Curve…

ASP .NET Page Life Cycle

Posted on: December 20, 2010

When you think about how a page is served by a web server to the browser and think of this process as the life cycle of a page, you can probably come up with a few important moments in the page’s life.

For example, the initial request by the browser is the starting point for the page’s “life.” Similarly, when the page has sent its entire HTML to the browser, its life may seem to end. However, more interesting events are going on in the page’s life cycle.

This article describes eight broad phases the page goes through. Within each phase, at least one event is raised that enables a page developer to hook into the page’s life cycle and perform actions at the right moment:-

Page request: A request to an ASPX page starts the life cycle of that page. When the web server is able and allowed to return a cached copy of the page, the entire life cycle is not executed. In all other situations, the page enters the start phase.

Start: In this phase, the page gets access to properties like Request and Response that are used to interact with the page’s environment. In addition, during this phase the PreInit event is raised to signal that the page is about to go into the initialization phase

Page initialization: During this phase, the controls you have set up in your page or added programmatically become available. Additionally, the Page class fires three events: Init, InitComplete, and PreLoad. Also during this phase, the control properties are loaded from View State and Control State again during a postback. So, for example, when you change the selected item in a DropDownList and then cause a postback, this is the moment where the correct item gets preselected in the drop-down listagain, which you can then work within your server-side code.

Load: During this phase the page raises the Load event.

Validation: In the validation phase, the Validation controls used to validate user input are processed.

Postback event handling: During this phase, the controls in your page may raise their own events. For example, the DropDownList may raise a SelectedIndexChanged event when the user has chosen a different option in the list. Similarly, a TextBox may raise the TextChanged event when the user has changed the text before she posted back to the server. When all event processing is done, the page raises the LoadComplete event. During this phase the PreRender event is raised to signal that the page is about to render to the browser. Shortly after that, SaveStateComplete is raised to indicate that the page is done storing all the relevant data for the controls in View State.

Rendering: Rendering is the phase where the controls (and the page itself) output their HTML to the browser.

Unload: The unload phase is really a clean-up phase. This is the moment where the page and controls can release resources like database connections. During this phase, the Unload event is raised so you can handle any cleanup you may need to do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Learning days (Calendar)

December 2010
    Jan »

Knowledge Bank (Archives)

I am on Twitter

Blog Stats

  • 532,900 hits

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 43 other followers

%d bloggers like this: