When a page request is sent to the Web server, the page is run through a series of events during its creation and disposal.
- PreInit
- Init
- InitComplete
- PreLoad
- Load
- Control event(s) – PostBack
- LoadComplete
- PreRender
- SaveStateComplete
- Render
- UnLoad
PreInit : The entry point of the page life cycle. Here we can access the master pages and themes. You can dynamically set the values of master pages and themes in this event. You can also dynamically create controls in this event.
Example : protected void Page_PreInit(object sender, EventArgs e) {}
Init : This event fires after each control has been initialized, each control's UniqueID is set and any skin settings have been applied. Here we can change initialization values.
EXAMPLE : protected void Page_Init(object sender, EventArgs e) {}
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
EXAMPLE : protected void Page_InitComplete(object sender, EventArgs e) {}
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
(1)Loads ViewState : ViewState data are loaded to controls
Note : The page viewstate is managed by ASP.NET and is used to persist information over a page roundtrip to the server. Viewstate information is saved as a string of name/value pairs and contains information such as control text or value. The viewstate is held in the value property of a hidden <input> control that is passed from page request to page request.
(2)Loads Postback data : postback data are now handed to the page controls
Note : During this phase of the page creation, form data that was posted to the server (termed postback data in ASP.NET) is processed against each control that requires it. Hence, the page fires the LoadPostData event and parses through the page to find each control and updates the control state with the correct postback data. ASP.NET updates the correct control by matching the control's unique ID with the name/value pair in the NameValueCollection. This is one reason that ASP.NET requires unique IDs for each control on any given page.
EXAMPLE : protected override void OnPreLoad(EventArgs e) {}
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.
EXAMPLE : protected void Page_Load(object sender, EventArgs e){ }
Control event(s)- (PostBack) : 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.These are the events, the code for which is written in your code-behind class(.cs file).
EXAMPLE : protected void Button1_Click(object sender, EventArgs e){ }
LoadComplete : This event signals the end of Load.
EXAMPLE : protected void Page_LoadComplete(object sender, EventArgs e){ }
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. For example : After this event, you cannot change any property of a button or change any viewstate value. Because, after this event, SaveStateComplete and Render events are called.
EXAMPLE :protected override void OnPreRender(EventArgs e){ }
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.
EXAMPLE : protected override void OnSaveStateComplete(EventArgs e){ }
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.
Note: Right click on the web page displayed at client's browser and view the Page's Source. You will not find any aspx server control in the code. Because all aspx controls are converted to their respective HTML representation. Browser is capable of displaying HTML and client side scripts.
EXAMPLE : protected void Page_Render(object sender, EventArgs e){ }
// Render stage goes here. This is not an event
(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. Cleanup can be performed on-
(a)Instances of classes i.e. objects
(b)Closing opened files
(c)Closing database connections.
EXAMPLE : protected void Page_UnLoad(object sender, EventArgs e) {}
06:59 |
Category:
ASP.NET
|
1 comments
Comments (1)
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging
Asp.Net Training In Chennai | Dot Net Training Center In Chennai | Dot Net Coaching Centers In Chennai
Best Software Testing Training Institute In Chennai | Software Testing Institutes In Chennai | Manual Testing Training In Chennai
Java Training Institute in Chennai | Core Java Training in Chennai | Java Course and Certification
PHP Course in Chennai | PHP Training Institute in Chennai | PHP Course and Certification