Loading, please wait...

A to Z Full Forms and Acronyms

Life Cycle in ASP.NET

In this article, we will learn how an ASP.NET page is delivered to a web browser when it is requested from the server and what mini processes take place in that time period.

Life Cycle in ASP.NET

When a user requests an ASP.NET page through a web browser(client) from the server, the page goes through a number of processes before actually being delivered to the Client. It includes initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.

When the request is made to the server, the response in the form of a page is stored in the server memory and then it is processed. After that, it is sent to the browser and unloaded from the memory.

The page life cycle has a set of general phases or stages that it goes through while the page is requested and delivered. These stages are explained below:

  • Page Request – The page request takes place before the page life cycle actually begins. At this stage, ASP.NET determines whether it is the first time that the page is requested or not. If it is a fresh page request, then it is compiled and then sent to the web browser. And if it is not requested for the very first time, the cached version of the page is delivered in response to the client.
  • Start - In this stage, the Request and Response objects are set. If the request is an old request or postback, the IsPostBack property of the page is set to true. The UICulture property of the page is also set.
  • Initialization – During the initialization phase, the controls on the page are available and their UniqueID property is set. A master page and themes are also applied to it if applicable.

If it is a postback, the data from the ViewState value is yet not restored in the control property values on the page.

  • Load – At this stage, control property values are set using the view state and control state values.
  • Validation - Validate method of the validation control is called and if it is successful, the IsValid property of the page is set to true.
  • Postback event handling - If the request is a postback (old request), the related event handler is invoked.
  • Rendering - At this stage, view the state for the page and all controls are saved. During the rendering stage, the page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Response property of the page so that a compilation can be made in a way that the user can understand.
  • Unload– The unload event is called after the rendering is completed and it is sent to the client. At this stage, the page properties such as Response and Request are unloaded and the memory is freed up.

There is a number of events that are called and take place while the page is requested and delivered to the web browser. An event handler is basically a function or subroutine, bound to the event, using declarative attributes such as Onclick or handle. These events are as follows:

  • PreInit : This event is raised just after the start stage is complete and before the initialization stage. PreInit is the first event in the page life cycle.

It checks the IsPostBack property and determines whether the page is a postback.

It also sets the themes and master pages, creates dynamic controls, and gets and sets profile property values.

This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.

  • Init : This event occurs after all controls have been initialized.We can use this event to read or initialize control properties.

This event can be handled by overloading the OnInit method or creating a Page_Init handler.

  • InitComplete: This event occurs at the end of the page initialization stage.
    We can use this event to make changes to view state that we want to make sure are persisted after the next postback.
  • OnPreLoad : This event occurs before the postback data is loaded in the controls.

This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.

  • Load : This event is raised for the page first time and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.

while loading an ASP.NET page, the page is loaded first in the memory which is then followed by the loading of controls. While in initialization, the controls are loaded first, and then the page is loaded.

  • Control events: This event is used to handle specific control events such as Button control' Click event.
  • LoadComplete: This event occurs at the end of the event-handling stage.
    We can use this event for tasks that require all other controls on the page to be loaded.

This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.

  • PreRender : This event occurs after the Page object has created all controls that are required in order to render the page.

The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.

  • PreRenderComplete : As the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.

              This event occurs after each data-bound control whose DataSourceID property is set calls its DataBind method.

  • SaveStateComplete : It is raised after view state and control state have been saved for the page and for all controls. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.
  • UnLoad : This event raised for each control and then for the page.

It raises the UnLoad event for all controls recursively and lastly for the page itself.

Final cleanup is done and all resources and references, such as database connections, are freed.

This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.

While Unloading, first the controls over the page are unloaded and after that, the page is unloaded from the server memory following the LIFO fashion.                                                                                                

A to Z Full Forms and Acronyms