13
SepUnderstanding ASP.NET Page Life Cycle
Asp.Net Page Life Cycle: An Overview
As the ASP.NET page runs, it goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.
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 AnswersWhat 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
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.
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.