ASP.NET framework provides an interface in the form of an Integrated Development Environment to create and execute web applications. A Web Application is a dynamic website made up of multiple web forms. ASP.NET Web Forms are the interfaces that are provided in a browser where a user can fill in the data in the controls and submit for processing or saving in a Database. Online examination forms, Internet banking, online shopping are a few examples where web forms collect user data. To create better web applications you need to understand the ASP.NET Page Life Cycle. Let’s Begin…
Phases of ASP.NET Page Life Cycle
Web Page Request
A page is requested by a user by typing its name in the address bar of browse. This is the starting point of an ASP.NET Page Life Cycle.
When a web page enters the start phase, Request and Response Properties of the page are set. At this time depending upon the type of request the IsPostBack Property is set if it is a postback request. If it is a new request then IsPostBack is unset.
Before displaying the ASP.NET web page the Page class creates a hierarchical tree of all the controls part of the page. All the components on the web page, excluding the directives, are part of this control tree. By Adding trace=” true” in the page directive the control tree can be seen. Each control’s UniqueID property is set by a unique ID. Controls are prepared for display by applying the themes. All the control properties get their view-state values.
Loading of the page
The controls that are assigned uniqueID in the Initialization phase of ASP.NET Web Page life cycle are instantiated. They are assigned the values using the view state and the control state. The user input values are evaluated and processed. If the page has been redisplayed then postback processing is done to update the page controls.
In this phase all the controls that are attached with validation control have the validation executed. When the data gets validated IsValid property of the page is set.
Postback Event Handling
When the request is checked and found to be an old request, the related postback event handler is executed.
This is the phase of giving the appearance to the requested page to be sent to the user’s browser as rendered HTML. View state for the page and its component controls is saved. The render method for each control is called by the Page. Rendering output is stored to the OutputStream class of the Response property of the page.
The rendered page is sent to the browser. All page properties including Response and Request are cleared as the unloading of a web page is done at the server’s end.
ASP.NET Page Life Cycle Events
At different phases on ASP.NET web page life cycle, the working of the page can be handled with different page events. The following events can be coded
|PreInit||Checks the IsPostBack property. Sets the themes and master pages Creates dynamic controls Gets and sets profile property values.|
|Init||initializes properties of controls builds the control tree for the page|
|InitComplete||tracking of view state|
|LoadViewState||loading view state information into the controls|
|LoadPostData||Contents of input fields in <form> tag are processed.|
|PreLoad||Fired before the postback data is loaded in the controls|
|Load||Starts with first page loads then loads child controls recursively|
|LoadComplete||Execution of control event handlers Execution of page validation|
|PreRender||Pages and controls update is performed before rendering|
|PreRenderComplete||Recursively fired for all child controls|
|SaveStateComplete Render||View state of controls are saved and any changes after this event are ignored Page controls’ States saved Personalization, control state and view state information saved HTML markup generated|
|Unload||UnLoad event for controls recursively and the page in the end Opened files closed Resources and references freed Database connections freed|
The ASP.NET Page Life Cycle gives a sneak peek into how a page is loaded and executed. With this knowledge, developers can customize form controls. They can initialize and populate control properties from view-state data. The behavior of controls can be managed when the user interacts with the webform.