ASP.Net Page life cycle overview

When an ASP.Net page runs, the page goes through a life cycle in which ir performs a series of processing steps. These includes

  • Initialization
  • Instantiating controls
  • Restroring and maintaining state
  • Running event handler code, and
  • Rendering

Why we need to learn ASP.Net Page life cycle?

It is important for you to understand the page life cycle so that you can write code at the appropriate life cycle stage for the effect you intended.

If you develop custom controls, you must be familiar with tha page life cycle in order to correctly initialize controls, populate control behaviour code. The life cycle of a control is based on page life cycle, and the page raises many of the events that you need to handle in a custom control.

General page life cycle stages:

Some parts of the life cycle occur only when a page is processed as postback. For postbacks, the page life cycle is same during a partial postback(as when you use an Update Panel control) as it is during a full page postback.

  1. Page Request
  2. Start
  3. Initialization
  4. Load
  5. Postback event handling
  6. Rendering
  7. Unload

1). Page Request:

The page request occurs before the page life cycle begins. When the page is requested by user, ASP.Net determines whether the page needs to be parsed and compiled(therefore beginning the life of a pge), or whether a cached version of the page can be sent in response without running the page.

2). Start:

In the start, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets UICultute property.

3). Initialization:

During page initialization, controls on the page are available and each controls UniqueID property is set. A master page and themes are also applied to the page if applicable. In the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.

4). Load:

During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

5). Postback event handling:

If the request is a postback, control event handlers are called. After that the validate method of all validation controls is called which sets the IsVaid property of individual validator controls and  of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)

6). Rendering:

Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.

7). Unload:

The Unload event is raised after the page has been fully rendered, sent to the client and is ready to be discarded. At this point, page properties such as Reponse and Request are unloaded and cleanup is performed.

Source: MSDN