X

What is Page Life Cycle in ASP.net?

ASP.NET is a web development platform designed and developed by Microsoft. It is a server-side technology. C sharp and ASP.NET is used to design web pages, web application, and web services.

You can develop ASP.NET application in 3 ways –

  1. Web Forms
  2. ASP.NET MVC
  3. ASP.NET Web Pages

ASP.NET Page Life Cycle:

Every programmer should understand ASP.NET page life cycle because he should know that where we have to place particular methods and when we have to set page related properties. Additionally, if we are developing custom server controls then clear understanding of page life cycle is very useful to correct initialization of control, setting properties with view-state data, and control’s code(Control events are subject to ASP.NET page only)

ASP.NET lifecycle specifies, how:

  • ASP.NET processes pages to produce dynamic output
  • The application and its pages are instantiated and processed
  • ASP.NET compiles the pages dynamically

The ASP.NET lifecycle divided into two parts:

– Application Life Cycle

– Page Life Cycle

In ASP.NET, page lifecycle includes various phases. These phases include initialization, instantiation, restoring and maintaining state running event handler code, and rendering. ASP.NET page life cycle is very important to understand to every developer to develop an application. All events, data processing, postback, rendering etc depends on page life cycle.

Page LifeCycle Stages:

  • Page Request: Page request event takes place before the page life cycle start. When the page request sends by the user, ASP.NET determine whether its need to parse or compile to that pages.
  • Start: In this stage request and response properties are set. Start stage also determines request type.
  • Initialization: In the initialization stage each control has set the unique property ID. Themes need to be initialized
  • Load: During load event, if the current request is a postback, control properties are set to utilize the view state and control state values.
  • Postback Event Handling: Control event handlers are called if the request is a postback.
  1. Example – If the postback is happening for button click then the button click event will fire.
  • Rendering: Before rendering all view state data has been set. The Page calls the Render method for every control. Render() method for all controls has been called and write the output on the output stream.
  • Unload:  The unload method takes after the page is fully loaded and is ready to terminate. At this point, rendered page is sent to the client and page properties such as Response and Request are unloaded and cleanup is done.

ASP.NET Page Life Cycle Events:

ASP.NET provides methods and events at each stage of the page lifecycle that we can use in our application. ASP.NET pages support the automatic wire-up event, which means it looks for methods with particular names and automatically runs those methods when particular events are raised.

Following are some events for ASP.NET page lifecycle:

  • PreInit: PreInit is the first event in ASP.NET page life cycle. This event is taking the start stage is complete and before the initialization stage. This event is used to set the Theme property dynamically.
  • Init: This event raised once all controls have been initialized. The use of this event is to read or initialize control properties. It helps to build up a tree of controls from the ASPX file.
  • InitComplete: This event takes at the end of the page’s initialization stage. In this event tracking of view state changes is turned on, until view state tracking is turned on, any values added to view state are lost across postbacks.
  • PreLoad: The PreLoad event is used for performing the page processing on the page before the control is loaded. We can process any kind of operation that needs to perform before page load.
  • Load: OnLoad methods controls called for each and every controls. Using this method we can set the control properties.
  • ControlEvents: Use these events to handle specific control events, such as a Button control’s Click event.
  • LoadComplete: This event can be handled by overloading the OnLoadComplete method. This event is used for tasks that require that all other controls on the page be loaded.
  • PreRender: Each control of the page has a PreRender event which is being invoked. If we want to change anything to any control this is the last event where we can do because after that PageRender starts.
  • PreRenderComplete: The PreRender event is fired for the child controls.
  • SaveStateComplete: In this event, any changes to the page or controls affect rendering, but the changes will not be retrieved on the next postback.
  • Render: Pages call the Render method for each and every control. The Render method generates the client-side HTML and a script which is necessary to properly display a control over the browser.
  • Unload: Unload event occurs at last in the ASP.NET page lifecycle. Unload event used to do a cleanup task like the closing of the open files, closing the database connections, or other requested specific tasks.
CodeKul: About CodeKul - Codekul is leading software development training institute in Pune which provides Online, Classroom & Video Series training to the Coders who are interested to learn things practically and want to solve problems in the world. Following Courses are Offered at CodeKul - - Mobile Application Development (Android, Kotlin, iOS) - Website Application Design & Development - Database Technology - Framework Technology - Software Testing - Unity3d / Game development - Full Stack Development - Mean Stack Development
Related Post