Saturday, 19 July 2014

Page Life Cycle Events in ASP.NET.



There are basically 11 Page Life Cycle Events in asp.net

1.     PreInit 
2.    Init
3.     InitComplete
4.     PreLoad
5.     Load
6.     Control (PostBack) event(s)
7.     LoadComplete
8.     PreRender
9.     SaveStateComplete
10.   Render
11.   UnLoad

Below are the details:

 


1. PreInit 

The entry point of the page life cycle is the pre-initialization phase called "PreInit". This is the only event where programmatic access to master pages and themes is allowed.  You can dynamically set the values of master pages and themes in this event.  You can also dynamically create controls in this event.

2. Init

This event fires after each control has been initialized, each control's UniqueID is set and any skin settings have been applied. You can use this event to change initialization values for controls. The "Init" event is fired first for the most bottom control in the hierarchy, and then fired up the hierarchy until it is fired for the page itself. 

  
3. InitComplete

Raised once all initializations of the page and its controls have been completed. Till now the viewstate values are not yet loaded, hence you can use this event to make changes to view state that you want to make sure are persisted after the next postback

(4)PreLoad

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

Loads ViewState : ViewState data are loaded to controls 


5. Load

The important thing to note about this event is the fact that by now, the page has been restored to its previous state in case of postbacks. Code inside the page load event typically checks for PostBack and then sets control properties appropriately. This method is typically used for most code, since this is the first place in the page lifecycle that all values are restored. Most code checks the value of IsPostBack to avoid unnecessarily resetting state. You may also wish to call Validate and check the value of IsValid in this method. You can also create dynamic controls in this method.

6. Control (PostBack) event(s)

ASP.NET now calls any events on the page or its controls that caused the PostBack to occur. This might be a button's click event or a dropdown's selectedindexchange event, for example.


7. LoadComplete

This event signals the end of Load.

8. PreRender

Allows final changes to the page or its control. This event takes place after all regular PostBack events have taken place. This event takes place before saving ViewState, so any changes made here are saved.


9. SaveStateComplete

Prior to this event the view state for the page and its controls is set.  Any changes to the page's controls at this point or beyond are ignored. 

10. Render

This is a method of the page object and its controls (and not an event). At this point, ASP.NET calls this method on each of the page's controls to get its output. The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser.


11. UnLoad

This event is used for cleanup code. After the page's HTML is rendered, the objects are disposed of. During this event, you should destroy any objects or references you have created in building the page. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object. 

No comments:

Post a Comment