ASP.NET - Life cycle

Page Life Cycle

ASP.NET is a web application framework developed by Microsoft. The life cycle of an ASP.NET application consists of a series of events that occur between the time when a request is made by a user and the time when a response is returned to the user. The life cycle can be divided into the following stages:

  1. Initialization: During this stage, the application domain is created, and the ASP.NET framework loads the required resources into memory. This stage involves creating instances of the HttpRuntime, HttpApplication, and HttpModule classes.
  2. Compilation: In this stage, the ASP.NET framework compiles the source code files into executable code that can be executed by the web server. The compiled code is then stored in the server's memory cache for faster access.
  3. Execution: This is the stage where the user request is processed. The framework executes the appropriate page or handler based on the user's request. The page execution involves the following sub-stages:
    1. Page Initialization: During this stage, the page's properties and controls are initialized, and the page's view state is loaded.
    2. Page Load: In this stage, the page's controls are loaded with data, and the page's events are fired.
    3. Postback Event Handling: In this stage, the page handles any postback events generated by the user.
    4. Page Rendering: During this stage, the page's HTML output is generated and sent to the user's browser.
  4. Cleanup: In this stage, the framework releases any resources that were used during the execution stage, including closing database connections and releasing memory.

Page Events Life Cycle

The ASP.NET Page Life Cycle consists of several events that occur during the processing of a web page request. Here is a detailed explanation of each event:

  1. Page_PreInit: This event occurs first in the page life cycle. During this event, the page's properties are set, including the MasterPageFile, Theme, and ViewStateMode. You can also create and add dynamic controls to the page during this event.
  2. Page_Init: During this event, the page's controls are initialized and their properties are set. This event is useful for creating and initializing objects that the page will use throughout its life cycle.
  3. Page_InitComplete: This event signals the completion of the page initialization stage. All controls on the page have been initialized and their properties set. At this point, the ViewState is loaded and can be accessed.
  4. Page_PreLoad: This event occurs just before the page is loaded with data. During this event, you can access the ViewState and other properties that were set during the initialization stage.
  5. Page_Load: This event occurs when the page is loaded with data. During this event, the page's controls are populated with data and their events are fired. This is the primary event where you would handle data binding and other data-related tasks.
  6. Page_LoadComplete: This event signals the completion of the page loading stage. At this point, all the page's controls have been loaded with data and their events have been handled.
  7. Page_PreRender: During this event, the page's controls are rendered to the page's output. You can modify the appearance of the page during this event.
  8. Page_PreRenderComplete: This event signals the completion of the page rendering stage. At this point, the page's HTML output is ready to be sent to the client.
  9. Page_SaveStateComplete: During this event, the ViewState and other state information are saved. This is the last event in the page life cycle before the page is unloaded.
  10. Page_Unload: This event signals the end of the page life cycle. At this point, all resources used by the page are released and the page is no longer available.