ASP.NET (4) - Life Cycle

This series of articles is about ASP.NET. They are from my previous years, legacy notes on OneNote. Reviewing the concepts may be helpful even for our current programming because they have a lot of similar features, such as Angular, React, and these modern "laguanges".

A. Introduction

This article will discuss the life cycle of an ASP.NET page associated with the Application Life Cycle and Control Life Cycle.

  • A - Introduction
  • B - ASP.NET Page Life Cycle Overview
  • C - Server Events Processing and Sequence
  • D - ASP.NET Application Life Cycle
  • E - Life Cycle of a Mobile Web Forms Page
  • F - Control Execution Lifecycle (.NET 1.1)

B. ASP.NET Page Life Cycle Overview

Following are the different phases of the Page Life Cycle:

unloadfile

Roughly

  • Start: Initialization
    • Init events of controls and then
    • The page
  • Load
    • Load view state,
    • Load the page and then
    • Load each child's controls
  • Even Handling
    • Postback handling,
    • Validation, and
    • Other events handling
  • Render
    • Save view state and
    • Render each controls and
    • The page
  • Unload: Clean up such as
    • Data connection or
    • Opened files

Shown as:

page methods

ASP.NET Page Life Cycle Overview

C. Server Events Processing and Sequence

  • Server Control Events:
    • Post-back events (posting events)
      • Cause the Web page to be sent back to the server for immediate processing, such as
        • Button,
        • LinkButton,
        • ImageButton).
    • Cached events 
      • Saved in the page’s view state to be processed when a post-back event occurs, such as:
        • TextBox,
        • DropDownList,
        • ListBox,
        • RadioButton and
        • CheckBox).
    • Validation events:
      • Handled on the page without posting back or caching, such as
        • Validators
  • Server Control Events Sequence:
    1. The client-side validation events are processed before the page is post back.
    2. The server-side validation events are processed first on the server.
    3. Then cached events;
    4. Then post-back event.

D. ASP.NET Application Life Cycle

  1. Application Start
    • The webserver executes the application start when a user requests an application for access. In this method, it sets all global variables to default.
  2. Object Creation
    • Object creation holds all the HTTP Context, HTTP Request, and HTTP Response by the webserver. It also contains information about the request, cookies, and browsing information.
  3. HTTP Application
    • HTTP Application is an object created by the webserver. It helps to process all the subsequent information that is sent to the user.
  4. Dispose
    • Dispose is an event that is called before the application is destroyed. It also helps to release manually unmanaged resources when the objects are no longer needed.
  5. Application End
    • Application End is the last stage of the application life cycle. It helps to unload the memory. 

E. Life Cycle of a Mobile Web Forms Page
 

Stage Page Event Overridable method
Page initialization Init  
View state loading LoadViewState  
Postback data processing LoadPostData method in any control that implements the IPostBackDataHandler interface  
Page loading Load  
Postback change notification aisePostDataChangedEvent method in any control that implements the IPostBackDataHandler interface  
Postback event handling Any postback event defined by controls RaisePostBackEvent method in any control that implements the IPostBackEventHandler interface
Page pre-rendering phase PreRender  
View state saving SaveViewState  
Page rendering Render  
Page unloading Unload  


F. Control Execution Lifecycle (.NET 1.1)
 

Phase What a control needs to do Method or event to override
Initialize Initialize settings needed during the lifetime of the incoming Web request (e.g, get application.path). See Handling Inherited Events. Init event (OnInit method)
Load view state At the end of this phase, the ViewState property of a control is automatically populated as described in Maintaining State in a Control. A control can override the default implementation of the LoadViewState method to customize state restoration. LoadViewState method
Process postback data Process incoming form data and update properties accordingly. See Processing Postback Data. Note   Only controls that process postback data participate in this phase. LoadPostData method (if IPostBackDataHandler is implemented)
Load Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data. See Handling Inherited Events. Load event (OnLoad method)
Send postback change notifications Raise change events in response to state changes between the current and previous postbacks. See Processing Postback Data. Note   Only controls that raise postback change events participate in this phase. RaisePostDataChangedEvent method (if IPostBackDataHandler is implemented)
Handle postback events Handle the client-side event that caused the postback and raise appropriate events on the server. See Capturing Postback Events. Note   Only controls that process postback events participate in this phase. RaisePostBackEvent method (if IPostBackEventHandler is implemented)
Prerender Perform any updates before the output is rendered. Any changes made to the state of the control in the prerendering phase can be saved, while changes made in the rendering phase are lost. See Handling Inherited Events. PreRender event (OnPreRender method)
Save state The ViewState property of a control is automatically persisted to a string object after this stage. This string object is sent to the client and back as a hidden variable. To improve efficiency, a control can override the SaveViewState method to modify the ViewState property. See Maintaining State in a Control. SaveViewState method
Render Generate output to be rendered to the client. See Rendering an ASP.NET Server Control. Render method
Dispose Perform any final cleanup before the control is torn down. References to expensive resources, such as database connections, must be released in this phase. See Methods in ASP.NET Server Controls. Dispose method
Unload Perform any final cleanup before the control is torn down. Control authors generally perform cleanup in Dispose and do not handle this event. UnLoad event (On UnLoad method)

References


Similar Articles