ASP.NET Application and Page Life Cycle Overview - Interview Question

Posted by: Pravinkumar Dabade , on 3/19/2016, in Category ASP.NET
Views: 126888
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.

aspnet-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 -

app-request-cycle

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 -

aspnet-page-lifecycle

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 -

aspnet-postback-lifecycle

  • 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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!