Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET lifecycle


May 12, 2021 ASP.NET


Table of contents


Life cycle

ASP.NET life cycle specifies how:

  • ASP.NET processing page to generate dynamic output
  • Applications and their pages are instantiated and processed
  • ASP.NET compile the page dynamically

ASP.NET life cycle can be divided into two groups:

  • The application lifecycle
  • Page lifecycle

ASP.NET application lifecycle

The application lifecycle has the following phases:

  • The user requests access to the application's resources, which are a page. T he browser sends this request to the web server.
  • A unified pipeline receives the first request and the following events occur:
    • An object creation for an ApplicationManager class.
    • An object of the Hosting Environment class is created to provide information resources.
  • Create a response object. Application objects such as HttpContext, HttpRequest, and HttpResponse are created and initialized.
  • An instance of an Http Application object is created and assigned to the request.
  • Requests are processed by the Http Application class. Different events raise such processing requests.

ASP.NET page lifecycle

When a page is requested, it is loaded into server memory and then processed and delivered to the browser. T hen uninstall it from memory. A t each of these steps, methods and events are available and can be rewritten as needed by the application. In other words, you can write your own code to replace the default code.

The page class creates a hierarchical tree for all controls on the page. A ll components on the page, except instructions, are part of the control tree. Y ou can see the control tree by adding trace s "true" to the page instructions. We override the page instructions and track them under 'directives' and 'event handling'.

The page lifecycle phase is:

  • Initialization
  • The page control is instantiated
  • State recovery and maintenance
  • The execution of the event processing code
  • The page is displayed

Understanding the page cycle helps us write code so that something specific can happen at any stage of the page lifecycle. It also helps write custom controls and initialize them at the right time, populates their properties with data in the view state, and runs code for control behavior.

Here are the different ASP.NET of a page:

  • Page Request - When ASP.NET gets a page request, it decides whether to parse and compile the page, or there will be a cached version of the page;
  • The opening of the page lifecycle - At this stage, set up the request and response objects. I f it is an old request or postback, the page IsPostBack property is set to correct. The page ULCulture property is also set.
  • Page Initialization - At this stage, the controls on the page are assigned to a unique ID and the theme is applied by setting the UniqueID property. For a new request, the postback data is loaded and the control properties are restorted to the value in the view state.
  • Page Load - At this stage, control properties are set, using view state and control state values.
  • Validation - Calls the validation method of the validation control and executes successfully, and the isValid property of the page is set to true.
  • Postback Event Processing - If the request is a postback (old request), the related event handler is called.
  • Page Display - At this stage, the view state of the page and all controls are saved. The page calls the display method for each control, and the rendered output is written to the OutputStream class in the page response property.
  • Uninstall - The displayed page is delivered to the client and page properties, such as responses and requests, are unloaded and all cleared.

ASP.NET page lifecycle events

At each stage of the page life cycle, the page raises some time and is encoded. An event handler is basically a function or sub-program, bound to an event, using declarations such as OnClick properties or processing.

Here are the page lifecycle events:

  • PreInit - PreInit is the first event in the page lifecycle. I t checks the IsPostBack property and determines whether the page is posted back. I t sets themes and master pages, creates dynamic controls, and gets and sets value profile property values. This event can be disposed of by overloading the OnPreInit method or by Page_PreInit the on-processing process.
  • The Init-Init event initializes the control properties and establishes the control tree. This event can be disposed of by overloading the OnInit method Page_Init creating an on-the-file handler.
  • The InitComplete - InitComplete event allows for tracking of view state. All controls turn on tracking in the view state.
  • The LoadViewState - LoadViewState event allows view state information to be loaded into the control.
  • LoadPostData - During this phase, the contents of all input fields defined by the label are processed.
  • PreLoad - PreLoad occurs before the postback data is loaded into the control. This event can be disposed of by overloading the OnPreLoad method Pre_Load creating an on-the-loop handler.
  • The Load-Load event is raised first by the page and then recursively raises all child controls. C ontrols in the control tree are created. This event can be disposed of by overloading the OnLoad method Page_Load creating an on-the-loop handler.
  • LoadComplete - The loading process is complete, the control event handler is running, and page validation occurs. This event can be disposed of by overloading the OnLoad method Page_LoadComplete creating an on-the-loop handler.
  • The PreRender - PreRender event occurs just before the output is displayed. By handling this event, pages and controls can perform any updates before the output is displayed.
  • PreRenderComplete - When the PreRender event is looped for all child controls, this event ensures that the previous stage is displayed.
  • SaveStateComplete - The page control state is saved. Personalization, control state, and view state information are saved.
  • The UnLoad-UnLoad phase is the last phase of the page lifecycle. I t loops through all controls to raise unLoad events, and finally to the page itself. T he final cleanup and release of all resources and references, such as database connections. This event can be disposed of by adjusting the OnLoad method Page_UnLoad creating an on-the-loop handler.