Updated April 10, 2023
Introduction to ASP.NET life cycle
ASP.Net is a software framework that enables the rapid development of software applications of any kind by providing reusable components. It can be installed on computers running Microsoft windows operating systems. there are two main components of the ASP.net framework, one is CLR (Common Language Runtime) and FCL (Framework base Class library). The CLR is used to locate, load, and manage .net types. the CLR also takes care of a number of low-level details such as memory management, threads, object context boundaries, and exception handling. In this topic, we are going to learn about the ASP.NET life cycle.
Asp.net life cycle
We can divide the asp.net lifecycle into application lifecycle and page life cycle. Let’s discuss this one by one.
Asp.net life cycle of the application is as follows
- The user first makes a request for a page on the website.
- The request is routed to the processing pipeline, which forwards it to the asp.net runtime.
- net runtime creates an instance of the ApplicationManager class. The ApplicationManager instance represents the .net framework domain that will be used to execute the request for your application. An appellation domain isolates global variables from other applications and allows each application to load and unload separately as required.
- After the application domain has been created an instance of the Hosting environment class is created. This class provides access to items inside the hosting environment, such as directory folders.
- net creates an instance of the core object that will be used to process the request. This includes HttpContext, HttpRequest, and HTtpResponse objects.
- net creates an instance of the HttpApplication class. this class is also the base class for the site’s global.asax file. This class is used to trap events that happen when your application starts or stops. When asp.net creates an instance of HttpApplication, it also creates the modules configured for the application such as SessionStateModule.
- Finally, asp.net processes request through the HttpApplication pipeline. This pipeline also includes a see of events for validating requests, mapping URLs, accessing the cache, and more. These events are of interest to developers who want to extend the application class.
Responding to application events
HttpApplication class provides several events you can handle to perform actions when asp.net raises certain events on the application level, the events won’t work on the pre-user level. following are the most common events used by developers.
- Application_Start: It is raised when your application is started by IIS.
- Application_End: It is raised when the tour application stops or shuts down.
- Application_Error: It is raised when an unhandled error occurs and rises up to the application scope.
- Application_LogRequest: It is raised when a request has been made to the application.
- Application_PostLogRequest: It is raised after the logging of the request has completed.
Asp.net life cycle of page
Apart from the application life cycle, there is also the page life cycle for each page on the website.
- The first user makes a request for the web page.
- On webserver, asp net compiles the page or pulls from the cache.
- Start: Set request and response objects. Determine IsPostBack.
- Init: Initialized Page controls but not their properties. Apply page theme.
- Load: If PostBack, load control properties from view State.
- Validation: Validate page and validator controls.
- Call control even handlers for PostBack requests.
- Rendering: Save view state. Render control and display the page.
- Unload: unload request and response objects. Perform clean-up. The page is ready to be discarded. Return response to the user.
Responding to page event
Following is the most common events used by developers
- Int: It is raised after each control has been initialized
- PreInt: This event is used only if you need to set the values such as the master page or theme.
- InitComplete: It is raised after all initialization of the page and its controls have been completed.
- PreLoad: It is raised before the view state has been loaded for the page and its controls and before postback processing.
- Load: Page load event is called first. Then the load event for each child control is called in turn.
- Control events: Asp.net calls any event on the page or its controls that caused the postback to occur. This might be button click event or drop-down list.
- LoadComplete: At this point, all control are loaded. If you need to do additional processing at this time, you can do so here.
- PreRender: It allows final changes to the page r its control. It takes place after all regular postback events have taken place.
- SaveStateComplete: It is useful if you need to write processing that requires the view state to be set.
- Render: asp.net calls this method on each of the page’s controls to get its output.
- Unload: This event is used for clean-up code. we can use it to manually release resources, a process that is rarely necessary.
This is a guide to ASP.NET life cycle. Here we discussed the asp.net life cycle in two phases application life cycle and page life cycle. We have also discussed the events. You may also look at the following article to learn more –