Introduction: ASP.NET is a powerful platform for building Web applications, that provides a tremendous amount of flexibilit and power for building !ust about an "ind of Web application. #ost people are familiar onl with the high level framewor"s li"e Web$orms and WebServices which sit at the ver top level of the ASP.NET hierarch. %n this article %&ll describe the lower level aspects of ASP.NET and explain how re'uests move from Web Server to the ASP.NET runtime and then through the ASP.NET (ttp Pipeline to process re'uests.
ASP.NET Application Life Cycle Wor"ing with ASP.Net applications re'uires wor"ing with the application)s life ccle and handling the appropriate events in the application)s lifeccle. Page events, (TTP(andlers and (TTP#odules are some of the common places we can loo" to handle various application life ccle scenarios. %n the following article we will see each of these. When an ASP.NET page executes, it undergoes a life ccle that includes various stages. The important stages in the page life ccle are %nitiali*ation, +oad, ,ontrol Events, -endering and .nload. The important features of each of these stages are/ Initialiation: 0uring this stage we can access the controls on the page but the control)s viewstate has not et been initiali*ed. Load: %n case of a postbac", the control)s properties have been initiali*ed from the viewstate. Control E!ents: %n case of a postbac", the control)s events are called. "endering: 0uring the rendering stage, the page calls the -ender method for each control and thus each control generates the (T#+ output that is merged with the resulting (T#+ of the page. #nload: The .nload event is raised after the page has been full rendered. %n this stage the generated page output has alread been sent to the client and the page can be discarded. ASP.Net Application Life Cycle: T$e application life cycle $as t$e following stages: .ser ma"es a re'uest for accessing application resource, a page. 1rowser sends this re'uest to the web server. A unified pipeline receives the first re'uest and the following events ta"e place/ An ob!ect of the Application #anager class is created. An ob!ect of the (osting Environment class is created to provide information regarding the resources. Top level items in the application are compiled. -esponse ob!ects are created. The application ob!ects/ (ttp ,ontext, (ttp -e'uest and (ttp -esponse are created and initiali*ed. An instance of the (ttp Application ob!ect is created and assigned to the re'uest. The re'uest is processed b the (ttp Application class. 0ifferent events are raised b this class for processing the re'uest. ASP.NET Page Life Cycle %!er!iew Each time a re'uest arrives at a Web server for an ASP.NET Web page, the first thing the Web server does is hand off the re'uest to the ASP.NET engine. The ASP.NET engine then ta"es the re'uest through a pipeline composed of numerous stages, which includes verifing file access rights for the ASP.NET Web page, resurrecting the user)s session state, and so on. At the end of the pipeline, a class corresponding to the re'uested ASP.NET Web page is instantiated and the Process-e'uest23 method is invo"ed 2see $igure 43. ASP.NET Page Life Cycle Life Cycle of Page Web page re'uest comes from browser. %%S maps the ASP.NET file extensions to ASPNET5%SAP%.0++, an %SAP% extension provided with ASP.NET. ASPNET5%SAP%.0++ forwards the re'uest to the ASP.NET wor"er process 2ASPNET5WP.E6E or W7P.E6E3. %SAP% loads (TTP-untime and passes the re'uest to it. Thus, (TTP Pipelining has begun. (TTP-untime uses (ttpApplication$actor to either create or reuse the (TTPApplication ob!ect. (TTP-untime creates (TTP,ontext for the current re'uest. (TTP,ontext internall maintains (TTP-e'uest and (TTP-esponse. (TTP-untime also maps the (TTP,ontext to the (TTPApplication which handles the application level events. (TTPApplication runs the (TTP#odules for the page re'uests. (TTPApplication creates (TTP(andler for the page re'uest. This is the last stage of (TTPipelining. (TTP(andlers are responsible to process re'uest and generate corresponding response messages. 8nce the re'uest leaves the (TTPPipeline, page level events begin. Page Events are as follows/ Pre%nit, %nit, %nit,omplete, Pre+oad, +oad, ,ontrol evetns 2Postbac" events3, +oad ,omplete, Pre-ender, SaveState,omplete, -ender and .nload. In t$is post& I am going to e'plain Page Life Cycle E!ents in brief. 9eneral Page +ife ,cle Stages/ Some parts of the life ccle occur onl when a page is processed as a postbac". $or postbac"s, the page life ccle is the same during a partial:page postbac" 2as when ou use an .pdatePanel control3 as it is during a full:page postbac". Page Life Cycle E!ents: PreInit() ,hec" for the %sPost1ac" propert to determine whether this is the first time the page is being processed. 4. ,reate or recreate dnamic controls. ;. Set master page dnamicall. 7. Set the Theme propert dnamicall. <. -ead or set profile propert values. %f -e'uest is postbac"/ The values of the controls have not et been restored from view state. %f ou set control propert at this stage, its value might be overwritten in the next event. Init() 4. %n the %nit event of the individual controls occurs first, later the %nit event of the Page ta"es place. ;. This event is used to initiali*e control properties. InitComplete() 4. Trac"ing of the =iewState is turned on in this event. ;. An changes made to the =iewState in this event are persisted even after the next postbac". PreLoad() 4. This event processes the postbac" data that is included with the re'uest. Load() %n this event the Page ob!ect calls the 8n+oad method on the Page ob!ect itself, later the 8n+oad method of the controls is called. Thus +oad event of the individual controls occurs after the +oad event of the page. ControlE!ents() This event is used to handle specific control events such as a 1utton control&s ,lic" event or a Text1ox control&s Text,hanged event. %n case of postbac" %f the page contains validator controls, the Page.%s=alid propert and the validation of the controls ta"es place before the firing of individual control events. LoadComplete() This event occurs after the event handling stage. This event is used for tas"s such as loading all other controls on the page. Pre"ender() %n this event the Pre-ender event of the page is called first and later for the child control. #sage: This method is used to ma"e final changes to the controls on the page li"e assigning the 0ataSource%d and calling the 0ata1ind method. Pre"enderComplete() This event is raised after each control)s Pre-ender propert is completed. Sa!eStateComplete() This is raised after the control state and view state have been saved for the page and for all controls. "enderComplete() The page ob!ect calls this method on each control which is present on the page. This method writes the control&s mar"up to send it to the browser. #nload() This event is raised for each control and then for the Page ob!ect.