Presentations

ASP.net Page Life Cycle Events - CodeProject

Description
ASP.net Page Life Cycle Events
Categories
Published
of 6
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
    Articles » Web Development » Wiki.ASP.NET articles » General ASP.NET Community , 11 Oct 21! PO# ASP.NET Pa$e #i%e &cle Events W'en a pa$e re()est is sent to t'e Web server, t'e pa$e is r)n t'ro)$' a series o% events*)rin$ its creation an* *isposal. +n t'is article, + ill E*itorial Note T'is articles as ori$inall& at iki.asp.net b)t 'as no been $iven a ne 'ome on o*ePro-ect. E*itin$ ri$'ts %or t'is article 'as been set at ron/e or above, so please $o inan* e*it an* )p*ate t'is article to keep it %res' an* relevant. When a page request is sent to the Web server, the page is run through a series of eventsduring its creation and disposal. In this article, I will discuss in detail the ASP.NET pagelife cycle Events   (1) PreInit  The entry point of the page life cycle is thepre-initialization phase called “PreInit”. This is the only event whereprogrammatic access to master pages and themes is allowed. You candynamically set the values of master pages and themes in this event. Youcan also dynamically create controls in this event. EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page using System;using System.Collections.Generic;using System.Linq;usingSystem.Web;using System.Web.UI;usingSystem.Web.UI.WebControls;   public partial class _Default :System.Web.UI.Page{   protected void Page_PreInit(object sender,EventArgs e)   {   // Use this event for thefollowing: // Check the IsPostBack property to determinewhether this is the first time the page is beingprocessed.   // Create or re-create dynamiccontrols.   // Set a master page dynamically.   // Set the Theme property dynamically.   }  0.2 3 votes4 ASP.NET Page Life Cycle Events - CodeProjecthttp://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...1 of 611/6/2014 9:38 PM  ------------------------------------ ------------------------------------(2)Init This event fires after each control has been initialized, eachcontrol's UniqueID is set and any skin settings have been applied. Youcan use this event to change initialization values for controls. The“Init” event is fired first for the most bottom control in thehierarchy, and then fired up the hierarchy until it is fired for thepage itself. EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected void Page_Init(object sender, EventArgs e) {// Raised after all controls have been initialized and any skinsettings have been applied. Use this event to read or initializecontrol properties.}  ------------------------------------ -------------------------------(3)InitComplete Raised once all initializations of the page and itscontrols have been completed. Till now the viewstate values are not yetloaded, hence you can use this event to make changes to view state thatyou want to make sure are persisted after the next postback  EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected void Page_InitComplete(object sender, EventArgs e){   // Raised by the Page object. Use this event forprocessing tasks that require all initialization be complete. } ------------------------------------ ------------------------------------(4)PreLoad Raised after the page loads view state for itself and allcontrols, and after it processes postback data that is included with theRequest instance (1)Loads ViewState  : ViewState data are loaded to controlsNote : The page viewstate is managed by ASP.NET and is used to persistinformation over a page roundtrip to the server. Viewstate informationis saved as a string of name/value pairs and contains informationsuch as control text or value. The viewstate is held in the valueproperty of a hidden <input> control that is passed from page request topage request.  (2)Loads Postback data  : postback data are now handed to thepage controls Note : During this phase of the page creation, form data that was postedto the server (termed postback data in ASP.NET) is processed againsteach control that requires it. Hence, the page fires the LoadPostDataevent and parses through the page to find each control and updates the ASP.NET Page Life Cycle Events - CodeProjecthttp://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...2 of 611/6/2014 9:38 PM  control state with the correct postback data. ASP.NET updates thecorrect control by matching the control's unique ID with the name/valuepair in the NameValueCollection. This is one reason that ASP.NETrequires unique IDs for each control on any given page.  EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected override void OnPreLoad(EventArgs e){   // Use this event if you need to perform processing onyour page or control before the Load event.   // Before thePage instance raises this event, it loads view state for itselfand all controls, and then processes any postback data includedwith the Request instance.} ------------------------------------ ------------------------------------(5)Load The important thing to note about this event is the factthat by now, the page has been restored to its previous state incase of postbacks. Code inside the page load event typicallychecks for PostBack and then sets control propertiesappropriately. This method is typically used for most code, sincethis is the first place in the page lifecycle that all values arerestored. Most code checks the value of IsPostBack to avoidunnecessarily resetting state. You may also wish to call Validateand check the value of IsValid in this method. You can also createdynamic controls in this method.   EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected void Page_Load(object sender, EventArgs e){   // The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, whichdoes the same for each of its child controls until the page andall controls are loaded.   // Use the OnLoad event method toset properties in controls and establish database connections.} ------------------------------------ ------------------------------------(6)Control (PostBack) event(s) ASP.NET now calls any events on the pageor its controls that caused the PostBack to occur. This might be abutton’s click event or a dropdown's selectedindexchange event, forexample.These are the events, the code for which is written in yourcode-behind class(.cs file).  EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected void Button1_Click(object sender, EventArgs e){   // This is just an example of control event.. Here it isbutton click event that caused the postback} ASP.NET Page Life Cycle Events - CodeProjecthttp://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...3 of 611/6/2014 9:38 PM  ------------------------------------ ---------------------------------(7)LoadComplete This event signals the end of Load.  EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected void Page_LoadComplete(object sender, EventArgs e){   // Use this event for tasks that require that all othercontrols on the page be loaded.} ------------------------------------ ----------------------------------(8)PreRender Allows final changes to the page or its control. This eventtakes place after all regular PostBack events have taken place. Thisevent takes place before saving ViewState, so any changes made here aresaved.For example : After this event, you cannot change any property ofa button or change any viewstate value. Because, after this event,SaveStateComplete and Render events are called.  EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected override void OnPreRender(EventArgs e){   // Each data bound control whose DataSourceID propertyis set calls its DataBind method.   // The PreRender eventoccurs for each control on the page. Use the event to make finalchanges to the contents of the page or its controls.} ------------------------------------ -----------------------------------(9)SaveStateComplete Prior to this event the view state for the page andits controls is set. Any changes to the page’s controls at this point orbeyond are ignored. EXAMPLE : Override the event as given below in your code-behind cs fileof your aspx page protected override void OnSaveStateComplete(EventArgs e){   // Before this event occurs, ViewState has been savedfor the page and for all controls. Any changes to the page orcontrols at this point will be ignored.   // Use this eventperform tasks that require view state to be saved, but that do notmake any changes to controls.} ------------------------------------ ------------------------------------(10)Render This is a method of the page object and its controls (and notan event). At this point, ASP.NET calls this method on each of thepage’s controls to get its output. The Render method generates theclient-side HTML, Dynamic Hypertext Markup Language (DHTML), and scriptthat are necessary to properly display a control at the browser. ASP.NET Page Life Cycle Events - CodeProjecthttp://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...4 of 611/6/2014 9:38 PM

pfe_chap26

Jul 23, 2017
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks