The ASP.NET life cycle specifies how to:
ASP.NET processes pages to generate dynamic output
The application and its pages are instantiated and processed
ASP.NET dynamically compiles pages
The ASP.NET life cycle can be divided into two groups:
Application life cycle
Page life cycle
The application life cycle has the following phases:
A user requests access to an application's resource, which is a page. The browser sends this request to the web server.
A unified pipeline receives the first request, and the following events occur:
An object of the ApplicationManager class is created.
An object of the HostingEnvironment class is created to provide the information resource.
Create a response object. Application objects such as HttpContext, HttpRequest and HttpResponse are created and initialized.
An instance of the HttpApplication object is created and assigned to the request.
Requests are handled by the HttpApplication class. Different events trigger such processing requests.
When a page is requested, the page is loaded into server memory, then processed and served to the browser. Then unload it from memory. At each of these steps, methods and events are available and can be adapted 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 of all controls on the page. All components on the page, except directives, are part of the control tree. You can see the control tree by adding trace = "true" to the page directive. We'll override the page directives and trace them under 'directives' and 'event handling'.
The page life cycle stages are:
initialization
Page control instantiation
State recovery and maintenance
Execution of event handling code
Page display
Understanding the page cycle helps us write code so that specific things can happen at any stage in the page life cycle. It also helps you write custom controls and initialize them at the right time, populate their properties with data from view state, and run code for the control's behavior.
Following are the different stages of an ASP.NET page:
Page Request - When ASP.NET gets a page request, it decides whether to parse and compile the page or have a cached version of the page; respond accordingly.
Start of page life cycle - In this phase, request and response objects are set up. If it is an old request or a postback, the page's IsPostBack property is set to correct. The page ULCulture property is also set.
Page initialization - During this phase, controls on the page are assigned unique IDs and themes are applied by setting the UniqueID property. For a new request, the postback data is loaded and the control properties are restored to their view state values.
Page Load - In this phase, control properties are set, using view state and control state values.
Validation - The validation method of the validation control is called and executed successfully, and the IsValid property of the page is set to true.
Postback event handling - If the request is a postback (old request), the relevant event handler is called.
Page Display - At this stage, the page's view state 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's response properties.
Unloaded - The displayed page is served to the client and page attributes, such as responses and requests, are unloaded and all cleared.
At each stage of the page's life cycle, the page fires some time and will be encoded. An event handler is basically a function or subroutine that is bound to an event, using declarative properties or handlers such as OnClick.
The following are page life cycle events:
PreInit - PreInit is the first event in the page life cycle. It checks the IsPostBack property and determines whether the page is posted back. It sets themes and home pages, creates dynamic controls, and gets and sets value profile property values. This event can be handled by overriding the OnPreInit method or creating a Page_PreInit handler.
Init - The Init event initializes control properties and builds the control tree. This event can be handled by overloading the OnInit method or creating a Page_Init handler.
InitComplete - The InitComplete event allows tracking of view state. All controls enable tracking in view state.
LoadViewState - The LoadViewState event allows loading view state information into a control.
LoadPostData - During this phase, the contents of all input fields defined by tags are processed.
PreLoad - PreLoad occurs before the postback data is loaded in the control. This event can be handled by overloading the OnPreLoad method or creating a Pre_Load handler.
Load - The Load event is raised first by the page and then recursively to 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.
LoadComplete - The loading process completes, control event handlers run, and page validation occurs. This event can be handled by overloading the OnLoad method or creating a Page_LoadComplete handler.
PreRender - The 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 raised for all child controls in a loop, this event ensures the completion of the pre-display phase.
SaveStateComplete - The page control state is saved. Personalization, control state, and view state information are saved.
UnLoad - The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls in a loop, and finally for the page itself. Finally completes cleaning and releasing all resources and references, such as database connections. This event can be handled by adjusting the OnLoad method or creating a Page_UnLoad handler.