ASP.NET Page Life Cycle is when a page is requested there are series of processes to load it on user side. These series of processes needs to be completed to load a page succesfully on user end. When a page is requested by user the page is loaded into the server memory and sent it to the user-end(browser). So these series processed like initialization, instantiation, restoring and maintaining state etc are performed on server. Developers needs to understand this ASP.NET page life cycle to override it on custom requirement.
Let’s look these processes/phases into details.
This phase occurs before ASP.NET page life begins. When a page is requested by user from the server.If it’s firt time server compile the page, parse it and send it to the user. If it’s not first time server will send the cache page to the user.
Start of ASP.NET page life cycle:
In this phase a Request and Response, two object are created. The request response carries all the information that was requested from the user and the Response object carries all the data that will be sent or render in the page. If the request from the user end is post back or a repeaded request IsPostBack page property will set to true and UICulture property is also set.
In this phase all page controls are initialized and Unique Id assigned to all controls by setting UniqueID property. If it’s a new request PostBack data is loaded and control properties are restored to view-state values.
In this stage the all the control properties of page are loaded with the default values. View state and control state values set all control properties.
There are some validation condition on the page sometimes. Let supprose there is a validation condition, text field should have some specific value. If this condition is not true there should be validation error. In validation control, Validate method is called and if its succesfully executed the IsValid property of page is set to true.
Postback event handling:
In this stage, the Postback event handler is called. This happens when the request is Post back or repeated request.
View state for all the controls and the page is saved before rendering. In rendering stage, Render method is called for each control, providing output to the OutputStream object of Response property of the page.
Once the completely rendered page is succesfully sent to the user there is no need of response and resquest data of that page. So in unloading process all Request and Response objects are removed from the server memory.
ASP.NET Life Cycle Events:
Multiple events are used in ASP.NET life cycle. These events can be customised by coding as per our application requirement. ASP.NET Life cycle events are functions that are bound to specific events.
This is the first event and it checks IsPostback property and recognises if the request is new or repeated. It sets master and theme pages and creates all dynamic controls of the page. We can handle this event by creating a Page_PreInit handler or overloading the OnPreInit method.
When all controls have been initialized this event occurs. Control properties can be read or initialized by this event. Init event comes first for the most bottom control in the hierarchy and occurs upword to the hierarchy, util it occurs for the page itself. We can handle this event by creating a Page_Init handler or oveloading OnInit method.
This event fired after the page initialization. Using this method you can change view state that you want to be presisted after the next postback. This event is raised by page object. We can handle this event by creating a Page_InitComplete handler.
This event occures before the viewstate or postback data is loaded for controls. We can handle this event by creating Page_PreLoad handler or overloading the OnPreLoad method.
In this event OnLoad method is called to load all the controls and the child controls until the page itself completely loaded. We can also call IsValid for validation in this method and can also create dynamic controls. We can use OnLoad event to establish database connections and set properties in the controlls. We can handle this event by creating Page_Load handler or overloading the OnLoad method.
Control Events are used to handle specifice control in a page like button control Click event. In the postback request if validator control exists it will check IsValid for the specific control before the further processing.
This event occurs at the end of event handling phase. We can use this event for the task that require all the controls loaded for its execution.
This event occurs when the page object load all controls that are required for the page rendering. This event occurs for the page and for all the child controls. “OnPreRender” allow developers to make final changes to the page or its control.
When each data bound control whose DataSourceID property is set calls its DataBind method this event occurs.
This event occurs when the viewstate and control state is saved for all the controls and page itself. In page or control any changes will be ignored at this point. This event can be used to perform any task that require view state to be saved but do not make any changes in controls.
This is a method not an event. The page object calls this method to load dynamic HTML to display control and content on browser.
This event helps to clean code. When all the processing is completed it is necessary to remove unnecessary objects and data from the memory to keep the application optimized. In Unload event we can remove object, close database connections and close open files. Thank you for reading.
Do not hesitate to comment below. MYCODEBIT team will try to respond ASAP.