ASP.NET Application and Page Life Cycle Overview - Interview Question
Posted by: Pravinkumar Dabade
in Category ASP.NET
Abstract: Lifecycle of an ASP.NET Page during its first request, as well as during Postback.
Often in interviews, you are asked to explain the Lifecycle of an ASP.NET Page during its first request, as well as during Postback. In this article, I will explain you some concepts which may prove helpful for you. Although this may not contain every detail, the knowledge gained will suffice for the interview.
An ASP.NET Application starts its life cycle when a user requests a page from the server using any preferred browser like Internet Explorer, Mozilla, Safari, Google Chrome etc. on web or mobile. Here’s an overview of the Application Lifecycle architecture.
When a request is sent by the browser to the web server [The default is Internet Information Service (IIS)], the request is sent to Internet Server Application Programming Interface (ISAPI). ISAPI is a collection of Windows-based web server services which are used used to modify and enhance the functionality provided by IIS. ISAPI checks the extension of a file which is requested by the browser and based on that, it loads the HttpHandler and module which is responsible for handling the request.
After this step, within the Application Pool, Application Manager creates a separate Application Domain for each web application. For example we can have two Application Domains, one for Web App A and one for Web App - B.
After creation of an Application Domain, ASP.NET creates an object of HttpRequest and HttpResponse for each request sent to the ASP.NET Web App. Once this is done, ASP.NET creates an object of HttpApplication which executes a number of events during the processing of the request as shown below -
Let’s quickly take an overview of the events based on Suprotim’s explanation of HTTP Request Lifecycle Events in IIS Pipeline that every ASP.NET Developer Should Know
- Begin Request - When the new request gets created, the Begin Request will get fired and it will be the first event which will be always raised during the request processing.
- Authenticate Request - This event confirms that the authentication configuration has authenticated the request and subscription to this event ensures that before processing the attached handler and module, it is authenticated.
- Authorize Request - This event confirms that the request has been authorized by ASP.NET. To implement the custom authorization, you can use this event.
- Resolve Request Cache - Once the authorization event gets completed, this event calls the caching module to serve the request from the cache, bypassing execution of the event handler.
- Map Request Handler - This event is used by ASP.NET framework to check the extension of the file and accordingly checks the handler for the request.
- Acquire Request State - When this event is raised, ASP.NET acquires the state information (Session State) that is associated with the request. In this case the request must has a valid Session ID.
- Execute Request Handler - When the handler generates the output, this event gets raised. This is the only request which is not exposed by the HttpApplication class.
- Release Request State - This event gets raised after ASP.NET finishes executing all request handlers. It also signals ASP.NET State modules to save the current request state.
- Update Request Cache - Once ASP.NET finishes the execution of event handlers, this event gets fired to let the caching modules store responses that will be reused to serve identical requests from the cache.
- Log Request - This request gets fired before ASP.NET performs any logging for the current request. This event can also be used to perform the custom logging.
Please note that, these are only some of the main events explained as per the above diagram.
Now let's see How Page Life Cycle works in ASP.NET. ASP.NET Page goes through a number of events when the page life cycle begins. These events can be used by developers to implement custom logic during the page execution life cycle. ASP.NET Page Life Cycle goes through the number of events in two different scenarios -
- Page Requested for the first time.
- Page requested during Post back (clicking of button etc.)
ASP.NET Page Lifecyle during first request
Let's see how the Page Lifecycle executes when the page is requested first time -
1. Page_PreInit Event - This event is the first event which gets executed during the page life cycle. You can use this event to change Master Page and Theme of the page. You can also change the Profile properties under this event.
2. Page_Init Event - This event gets fired when all the controls of the page are initialized. Developers can change the properties of a control in this event.
3. Page_InitComplete - This event is raised by the Page Object where you can perform different operations to complete the initialization.
4. Page_PreLoad Event - This event is used for performing operations on page or controls before load event gets fired.
5. Page_Load Event - This event loads all the controls and their children controls. You can use this event for writing database logic or filling the data into Dropdown list control. Developers can perform custom logic against a control, like setting control properties.
6. Page_LoadComplete - This event can be used for loading all the other controls.
7. Page_PreRender - This event can be used for making final changes to the page properties or control properties.
8. Page_SaveStateComplete Event - Before this event, ViewState created for the controls and page is saved with Key-Value pair. It is saved in a HiddenField. Do not use this event for changing the control or page properties as they will be ignored.
9. Page_Render Method - This is a page method and not an event. Each server side control has this event which generates the mark-up for rending it on a browser.
10. Page_Unload Event - This event unloads all the controls and then the page. Developers can use this method for custom logging or any cleanup operations for controls and page.
ASP.NET Page Lifecycle after post back
Most of the event are same when the Page post back lifecycle gets executed. But in addition to the events we just saw, it performs some other events as well. Take a look at the following diagram, especially the ones with an orange border -
- Page_LoadState Event - During the round trips of client-to-server and server-to-client, ASP.NET maintains the state of the controls and the page, in the form of ViewState. ViewState contains the control ID and the value of the control, as a key-value pair. This ViewState is loaded during the post back of the request.
- Page_ProcessPostData Event - This event processes the controls state by updating the control values with the data which is post back by the request.
- Server Control Events - Here all the events of the controls will be processed like Button click event, Textbox TextChanged event etc.
And that’s pretty much you have to explain to impress your interview when he/she asks you to explain the ASP.NET Application and Page Life Cycle.