Understanding ASP.NET Page Life Cycle

Understanding ASP.NET Page Life Cycle

26 Apr 2024
Beginner
83 Views
5 min read

Asp.net page life cycle: An Overview

ASP.NET page runs, the page goes through a life cycle in which it performs a series of various processing steps. In this ASP.NET tutorial, we will explore more about the Asp.net page life cycle and its different processing steps. Also, consider learning the ASP.NET Core Course for a better understanding of .net concepts. Now Let's see what the Asp.net page life cycle is.

Note: If you are preparing for a .Net interview  read the following article:

Top 50 .NET Interview Questions and Answers

What is the Asp.net page life cycle?

  • The asp.net page life cycle begins, When a page is requested, it is loaded into the server memory, processed, and sent to the given browser.
  • After that, it will unloaded from the memory.
  • At each of these loading page steps, methods and events are present, which could be overridden according to the needs of the software.
  • there are various page life cycle phases such as initialization, Instantiation, Restoration, Execution, and page rendering.
  • Understanding the page cycle makes a difference in building codes for making a few particular things happen at any step of the page life cycle.
  • It also makes a difference in writing custom controls and initializing them at the proper time.
  • It populates their properties with view-state information, and running control behavior code. 

Different stages of an ASP.NET page life-cycle:

The following chart shows the various stages of the page life cycle

Asp.net page life cycle

Different ASP.NET Page Life Cycle Events

We have seen now the different stages of an ASP.NET page life-cycle at each stage, the page raises some events, Let's discuss them one by one.

Different ASP.NET Page Life Cycle Events

1.PreInit

  • First, Check the IsPostBack property, whether this is the first time the page is being processed or not .
  • Then, Create or re-create dynamic controls.
  • Now, dynamically set a master page.
  • Also, Set the Theme property dynamically.

2. Init

  • This event fires after each control has been initialized.
  • Each control has its UniqueID set.
  • And any skin settings have been applied.
  • We Used this event to read or initialize control properties.
  • This event is fired first for the bottom-most control in the hierarchy and then fired up the hierarchy until it is fired for the page itself.

3.InitComplete

  • This event allows us to track of view state.
  • Its, All the controls turn on view-state tracking.

4.LoadViewState

  • This stage is also called OnPreLoad
  • Raised after the page loads view state for itself and all controls and after it processes postback data that is included with the Request instance.
  • It loads the view state for itself and all controls and then processes any postback data included with the Request instance,Before the Page instance raises this event,
  • The ViewState data are loaded to controls.
  • Then the Postback data are now handed to the page controls, This process called Loads Postback data

5.PreLoad 

  • It occurs before the post-back data is loaded in the controls.
  • The PreLoad event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.

6. Load

  • The Load event is raised for the page first and then recursively for all child controls.
  • Hence, The controls in the control tree are created.
  • This event can be managed by overloading the OnLoad method or creating a Page_Load handler.

7.LoadComplete 

  • After completing the loading process, control event handlers run, and page validation takes place.
  • LoadComplete event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.

8.PreRender 

  • This event occurs right before the output is rendered.
  • By handling PreRender events, pages, and controls can perform any updates before the output is rendered.

9.PreRenderComplete

  • As this event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.

10.SaveStateComplete

  • At this stage, the State of control of the page is saved.
  • During this event, Personalization, control state, and view state information are saved.
  • The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.

11.UnLoad 

  • This phase is the last phase of the page life cycle.
  • It raises the UnLoad event for all controls recursively and lastly for the page itself.
  • When Final cleanup is done all resources and references, such as database connections, are freed.
  • This event is handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.
Conclusion:

So in this article, we have learned about the Asp.net life cycle. I hope you enjoyed learning these concepts while programming with .Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Consider our .NET Certification Training to learn .net from scratch.

FAQs

Q1. What is the ASP.NET page life cycle?

It includes initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.

Q2. What is the life cycle of web page?

the sequence of events that happen between the request (hitting the page) and response (returning data to the user).

Q3. How does ASP page work?

Working: When a browser requests an ASP or ASP.NET file, the ASP engine reads the file, executes any code in the file, and returns the result to the browser.

Take our free aspnet skill challenge to evaluate your skill

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET CHALLENGE

Share Article
Live Training Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this