All IT Courses 50% Off
Dotnet Tutorials

ASP.NET Application & Page Life Cycle

ASP.NET life cycle determines

  • How pages are processed to return dynamic output.
  • How the application and pages are initialized
  • How the asp pages are compiled dynamically.

The ASP.NET life cycle is categorized into two parts:

  1. Application Life Cycle
  2. Page Life Cycle

ASP.NET Application Life Cycle:

When a user requests for accessing a page, this request is sent by the browser to the webserver. A pipeline receives the request and creates an object of class ApplicationManager and an object of class HostingEnvironment to provide the information regarding the resources. When the top-level items are compiled, response objects are created. The application objects like HttpContext, HttpRequest, and HttpResponse are also created. An instance of HttpApplication is then created and assigned to the request.

https://www.tutorialandexample.com/wp-content/uploads/2020/01/What-is-ASP.Net-Lifecycle.png

The above image shows the stages of an application life cycle.

Stage 1: Application Start: This is the initial stage of the life cycle. The application starts as soon as the user makes a request to the server. This happens when the user goes to the homepage for the first time. At this time, a method called Application_start starts executing by the server. In this method, default values are assigned to the global variables.

All IT Courses 50% Off

Stage 2: Object Creation: The second stage of the application is object creation. Object of HttpRequest, HttpResponse, and HttpContext are created. The HttpContext is a container class of HttpRequest and HttpResponse. The HttpRequest contains information about the current request that also includes cookies and browser information. The HttpResponse contains the response returned by the server.

Stage 3: HttpApplication Creation: This object is created by the webserver that is used to process each request sent to the server. Suppose there are two web applications, one is a shopping cart, and the other is the news website. We will have two objects of HttpApplication in this case.

Stage 4: Dispose: This method is used to release the resources. This is called before the application instance is destroyed.

Stage 5: Application End: This is the final stage of the life cycle, where the application is unloaded from the memory.

ASP.NET Page Life Cycle: 

When a page is requested, it gets loaded into the server memory. It is then processed and sent to the browser. Then it is unloaded from the memory.

Page Life Cycle helps in writing custom controls and initializing them at the right time. The page class creates a hierarchical tree that contains all the controls of the page. The controls can be seen by adding trace=”true” to the page directive.

https://www.tutorialandexample.com/wp-content/uploads/2020/01/What-is-ASP.Net-Lifecycle2.jpg

The above image shows the different stages of a page life cycle.

Stage 1: Page Request: When the page is requested for the first time from the server, the server checks whether the request is for the first time. If the server finds that the request is for the first time, then it compiles the page, parses the response, and sent it to the user. If the request is not the first time, then it checks the cache to find the page output and then return to the user.

Stage 2: Page Start: At this stage, two objects, Request object, and Response object are created. The Request object contains all the information, which was sent when the page is requested. The Response object contains all the information that is sent back to the user. If the request sent by the user is an old request or posts back, then the IsPostBack property is set to true. UICulture property of the web page is also set.

Stage 3: Page Initialization: At this stage, all the controls, such as labels, text boxes, radio buttons that are available on a web page, are initialized. The UniqueID property of all the controls is also set.

Stage 4: Page Load: At this stage, all the default values are loaded. For example, if the text box has a default value, then it is loaded during the page load time.

Stage 5: Validation: Sometimes, the validation set is also present in a form. If the validation condition fails, then the error will be returned during the page load time.

Stage 6: Postback Event Handling: If the same page is loaded again, this event triggers. For example, while clicking on the submit button, if the same page appears, then this Postback event handler is called.

Stage 7: Page Rendering: This happens just before sending the response to the user. All the response is first saved, and then the result is sent to the user.

Stage 8: Unload: When the page output is sent to the user, then we need to release the memory. The unloading process removes all unwanted objects from memory.

Facebook Comments

Related Articles

Back to top button