12.RenderControl(this.CreateHtmlTextWriter(this.Response.Output))

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

前言asp.net页面运行时候,页面将经历一个生命周期,在生命周期中将执行一系列的处理步骤。包括初始化、实例化控件、还原和维护状态、运行时间处理程序代码以及进行呈现。熟悉页面生命周期非常重要,这样我们才能在生命周期的合适阶段编写代码。如果我们能在写代码的时候想着我们现在是在做生命周期的哪一步那将是非常好的。

你可能会说我不清楚还不是一样写代码,反正每次都在Page_load里面写代码 然后页面就出来了我管那么多干什么。所谓知其然如果能知其所以然岂不是更吊?我个人认为做ASP.NET B/S开发只要咱们熟悉ASP.NET页面生命周期,熟悉“请求-处理-响应“模式,对于一时半儿没有触及到的细节,等真正遇到那个点再去理解它也是毫不费力的。

几个代表性的问题在开始的时候我们先思考几个问题,看看我们在描述完页面生命周期的时候,能不能回答上这几个问题

1.为什么在服务器端能通过this.textbox1.Text获取到用户提交过来的数据?

2.在Page_Load中Response.Write("hello")查看生成的html代码原文件,hello在哪里?为什么?

3.有一个服务器端的按钮,设置了点击事件,该点击事件什么时候执行?是先执行Page_Load事件还是先执行点击事件?

4.为什么在服务器端通过this.textbox1.Text设置值后,客户端就能显示出来?

看过我上一篇博客【深入ASP.NET原理系列】--ASP.NET请求管道、应用程序生命周期、整体运行机制 童鞋可能知道,ASP.NET应用程序周期中PReRequestHandlerExecute事件与PostRequestHandlerExecute事件之间就是我们的页面生命周期了,对于aspx页面就是一系列的打造页面控件树,触发各种页面时间,对于一般处理程序ashx就是直接执行咱们开发者写的ProcessRequest方法了,对于MVC应用程序就是创建控制器工厂,创建控制器对象,调用Action那一套了。

下面主要讲述的就是ASP.NET WebForm中的页面的生命周期了。

[flash=500,400]http://s1.knowsky.com/20160211/gdsw4dkvkzb13.png[/flash]

我们用反编译工具查看Page类的ProcessRequest方法可以看见先调用了FrameworkInitialize; FrameworkInitialize里面就是打造了页面控件树,然后调用了ProcessRequestMain,就开始了执行整个页面生命周期了(其实就是调用了一系列的事件方法)(可能部分图看不见右边,可在新标签页中打开图片)

1.打造页面控件树FrameworkInitialize内部调用了_buildControlTree()方法

[flash=500,400]http://s1.knowsky.com/20160211/fjgltbjvxec14.png[/flash]

上图中左边是前台页面的代码,右边是对应 生成的打造控件树的代码。中间截取的是生成表单那一部分的代码。

下面看一张原理图

[flash=500,400]http://s1.knowsky.com/20160211/mpddbsoywpg14.png[/flash]

浏览器的DOM树是根据Html标签生成一个C语言的DOM树,而ASP.NET服务器端是用C#打造的一个控件树,也是按照DOM结构打造的。本质是一样。服务器端所有东西都加到页面对象的控件集合中去了。标签在服务器端有对应的控件对象的时候就用控件对象,没有的时候就使用LiteralControl进行封装。不管是服务器控件还是字符串标签(没有runat="server"的标签)都以控件对象的方式存在前台页面类的控件集合里面。好处就是生成前台页面的html代码的时候,只需要遍历控件集合里面的每一个控件对象的RenderControl方法,每一个控件都会调用自己的Render方法生成对应的Html字符串。那么所有控件的生成的html字符串就还原成一个页面的html代码了。

2.触发PerformPreInit事件

在所有初始化之前初始化了这个事件,这个事件主要是初始化了主题,初始化了母版页

[flash=500,400]http://s1.knowsky.com/20160211/erlqpgwrpq514.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/tgbs5rwz0ko14.gif[/flash]1privatevoidPerformPreInit()23{45this.OnPreInit(EventArgs.Empty);67this.InitializeThemes();89this.ApplyMasterPage();1011this._preInitWorkComplete =true;1213}

View Code

3.触发InitRecursive事件[flash=500,400]http://s1.knowsky.com/20160211/uaiyc4s041314.png[/flash]

4.触发LoadAllState()事件加载页面状态解析ViewState,将页面表单中的ViewState进行反Base64编码,反序列化,存在页面的ViewState属性中

5.触发ProcessPostData(this._requestValueCollection, true)事件

[flash=500,400]http://s1.knowsky.com/20160211/erlqpgwrpq514.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/tgbs5rwz0ko14.gif[/flash]1privatevoidProcessPostData(NameValueCollection postData,boolfBeforeLoad)2{3if(this._changedPostDataConsumers ==null)4{5this._changedPostDataConsumers =newArrayList();6}7if(postData !=null)8{9foreach(stringstrinpostData)10{11if((str !=null) && !IsSystemPostField(str))12{13Control control =this.FindControl(str);14if(control ==null)15{16if(fBeforeLoad)17{18if(this._leftoverPostData ==null)19{20this._leftoverPostData =newNameValueCollection();21}22this._leftoverPostData.Add(str,null);23}24}25else26{27ipostBackDataHandler postBackDataHandler =control.PostBackDataHandler;28if(postBackDataHandler ==null)29{30if(control.PostBackEventHandler !=null)31{32this.RegisterRequiresRaiseEvent(control.PostBackEventHandler);33}34}35else36{37if(postBackDataHandler !=null)38{39NameValueCollection postCollection = control.CalculateEffectiveValidateRequest() ?this._requestValueCollection :this._unvalidatedRequestValueCollection;40if(postBackDataHandler.LoadPostData(str, postCollection))41{42this._changedPostDataConsumers.Add(control);43}44}45if(this._controlsRequiringPostBack !=null)46{47this._controlsRequiringPostBack.Remove(str);48}49}50}51}52}53}54ArrayList list =null;55if(this._controlsRequiringPostBack !=null)56{57foreach(stringstr2inthis._controlsRequiringPostBack)58{59Control control2 =this.FindControl(str2);60if(control2 !=null)61{62IPostBackDataHandler adapterInternal = control2.AdapterInternalasIPostBackDataHandler;63if(adapterInternal ==null)64{65adapterInternal = control2asIPostBackDataHandler;66}67if(adapterInternal ==null)68{69object[] args =newobject[] { str2 };70thrownewHttpException(SR.GetString("Postback_ctrl_not_found", args));71}72NameValueCollection values2 = control2.CalculateEffectiveValidateRequest() ?this._requestValueCollection :this._unvalidatedRequestValueCollection;73if(adapterInternal.LoadPostData(str2, values2))74{75this._changedPostDataConsumers.Add(control2);76}77}78elseif(fBeforeLoad)79{80if(list ==null)81{82list =newArrayList();83}84list.Add(str2);85}86}87this._controlsRequiringPostBack =list;88}89}9091929394

View Code

主要做了两件事

1)将表单里提交过来的控件数据设置给页面对象的控件树中对应控件的属性(给前面打造的控件树里面控件给值),这样在服务器端就可以拿到客户端输入的值了。

2)将表单里面提交过来的值与ViewState中控件原来的值进行比对,不同则表示要触发该控件的Change 事件,则同时将该控件放到一个集合(看源码其实就是changedPostDataConsumers)中。在后续执行过程中遍历改集合依次触发对应控件的Change事件。

6.触发LoadRecursive()事件[flash=500,400]http://s1.knowsky.com/20160211/k1sbzrskcxn15.png[/flash]

大名鼎鼎的Page_Load就是在这里执行的。不过是先执行页面本身的Load事件再执行页面控件的Load事件哦,这时候前面给控件赋的值,表单提交过来的数据,ViewState等等都可以使用了,IsPostBack的原理就是判断是否有name为__VIEWSTATE的数据提交过来

7.再次触发ProcessPostData(this._leftoverPostData, false)事件这个事件我在网上看了很多人说是将第一次遗漏下来的,第一次执行ProcessPostData没有涉及到的控件进行处理,但是并没有说明哪些遗漏下来了。为什么第一次没处理了? 最后Google查到是处理我们开发者在页面的Page_Load方法中添加的控件。在Page_Load中我们可以自己创建控件对象加到页面对应的“C#DOM树中“,如:在Page_Load中写

TextBox txt = new TextBox();txt.ID ="myTxtBox";this.form1.Controls.Add(txt);

这就是把开发者自己创建的控件加在页面的form1的表单中。当然你也可以加上Change事件了创建控件的时候。执行的还是上面那两件事了。则回发的时候可以给开发者在Page_Lod中手动创建的控件还原值。

8.触发RaiseChangedEvents事件[flash=500,400]http://s1.knowsky.com/20160211/ykbt13egdva15.png[/flash]

循环遍历changedPostDataConsumers集合中的所有控件,依次执行控件的非点击回传事件,比如文本框的改变事件等

9.触发RaisePostBackEvent(this._requestValueCollection)事件[flash=500,400]http://s1.knowsky.com/20160211/mevf0b4ixvx15.png[/flash]

执行按钮点击回传事件或者验证事件,如果有多个按钮,根据回发过来的按钮的 name来判断触发哪个按钮的事件,或者触发该控件的验证事件

10.触发PerformPreRenderComplete事件

循环遍历控件树中所有的控件,根据每个控件生成对应的Html代码,把服务器控件渲染成普通的html控件。

11.触发事件SaveAllState事件将服务器端ViewState集合中的内容(开发者自己加的数据或控件状态信息等)序列化然后Base64编码然后设置到客户端隐藏域__ViewState中

12.RenderControl(this.CreateHtmlTextWriter(this.Response.Output))把要发送到客户端浏览器的内容设置到Response.Output,应用程序将它发送 到客户端浏览器。

看到这里不知道大家是否已经可以清晰地回答开篇提到的几个问题了,其实就是这些事件执行的先后顺序,页面生命周期了。”前人植树,后人乘凉了"

最后附上生命周期ProcessRequest源码

[flash=500,400]http://s1.knowsky.com/20160211/erlqpgwrpq514.gif[/flash][flash=500,400]http://s1.knowsky.com/20160211/tgbs5rwz0ko14.gif[/flash]1privatevoidProcessRequestMain(boolincludeStagesBeforeAsyncPoint,boolincludeStagesAfterAsyncPoint)2{3try4{5HttpContext context =this.Context;6stringstr =null;7if(includeStagesBeforeAsyncPoint)8{9if(this.IsInAspCompatMode)10{11AspCompatapplicationStep.OnPageStartsessionObjects();12}13if(this.PageAdapter !=null)14{15this._requestValueCollection =this.PageAdapter.DeterminePostBackMode();16if(this._requestValueCollection !=null)17{18this._unvalidatedRequestValueCollection =this.PageAdapter.DeterminePostBackModeUnvalidated();19}20}21else22{23this._requestValueCollection =this.DeterminePostBackMode();24if(this._requestValueCollection !=null)25{26this._unvalidatedRequestValueCollection =this.DeterminePostBackModeUnvalidated();27}28}29stringcallbackControlID =string.Empty;30if(this.DetermineIsExportingWebPart())31{32if(!RuntimeConfig.GetAppConfig().WebParts.EnableExport)33{34thrownewInvalidOperationException(SR.GetString("WebPartExportHandler_DisabledExportHandler"));35}36str =this.Request.QueryString["webPart"];37if(string.IsNullOrEmpty(str))38{39thrownewInvalidOperationException(SR.GetString("WebPartExportHandler_InvalidArgument"));40}41if(string.Equals(this.Request.QueryString["scope"],"shared", StringComparison.OrdinalIgnoreCase))42{43this._pageFlags.Set(4);44}45stringstr3 =this.Request.QueryString["query"];46if(str3 ==null)47{48str3 =string.Empty;49}50this.Request.QueryStringText =str3;51context.Trace.IsEnabled =false;52}53if(this._requestValueCollection !=null)54{55if(this._requestValueCollection["__VIEWSTATEENCRYPTED"] !=null)56{57this.ContainsEncryptedViewState =true;58}59callbackControlID =this._requestValueCollection["__CALLBACKID"];60if((callbackControlID !=null) && (this._request.HttpVerb ==HttpVerb.POST))61{62this._isCallback =true;63}64elseif(!this.IsCrossPagePostBack)65{66VirtualPath path =null;67if(this._requestValueCollection["__PREVIOUSPAGE"] !=null)68{69try70{71path = VirtualPath.CreateNonRelativeAllowNull(DecryptString(this._requestValueCollection["__PREVIOUSPAGE"], Purpose.WebForms_Page_PreviousPageID));72}73catch74{75this._pageFlags[8] =true;76}77if((path !=null) && (path !=this.Request.CurrentExecutionFilePathObject))78{79this._pageFlags[8] =true;80this._previousPagePath =path;81}82}83}84}85if(this.MaintainScrollPositionOnPostBack)86{87this.LoadScrollPosition();88}89if(context.TraceIsEnabled)90{91this.Trace.Write("aspx.page","Begin PreInit");92}93if(EtwTrace.IsTraceEnabled(5,4))94{95EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER,this._context.WorkerRequest);96}97this.PerformPreInit();98if(EtwTrace.IsTraceEnabled(5,4))99{100EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE,this._context.WorkerRequest);101}102if(context.TraceIsEnabled)103{104this.Trace.Write("aspx.page","End PreInit");105}106if(context.TraceIsEnabled)107{108this.Trace.Write("aspx.page","Begin Init");109}110if(EtwTrace.IsTraceEnabled(5,4))111{112EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER,this._context.WorkerRequest);113}114this.InitRecursive(null);115if(EtwTrace.IsTraceEnabled(5,4))116{117EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE,this._context.WorkerRequest);118}119if(context.TraceIsEnabled)120{121this.Trace.Write("aspx.page","End Init");122}123if(context.TraceIsEnabled)124{125this.Trace.Write("aspx.page","Begin InitComplete");126}127this.OnInitComplete(EventArgs.Empty);128if(context.TraceIsEnabled)129{130this.Trace.Write("aspx.page","End InitComplete");131}132if(this.IsPostBack)133{134if(context.TraceIsEnabled)135{136this.Trace.Write("aspx.page","Begin LoadState");137}138if(EtwTrace.IsTraceEnabled(5,4))139{140EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER,this._context.WorkerRequest);141}142this.LoadAllState();143if(EtwTrace.IsTraceEnabled(5,4))144{145EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE,this._context.WorkerRequest);146}147if(context.TraceIsEnabled)148{149this.Trace.Write("aspx.page","End LoadState");150this.Trace.Write("aspx.page","Begin ProcessPostData");151}152if(EtwTrace.IsTraceEnabled(5,4))153{154EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER,this._context.WorkerRequest);155}156this.ProcessPostData(this._requestValueCollection,true);157if(EtwTrace.IsTraceEnabled(5,4))158{159EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE,this._context.WorkerRequest);160}161if(context.TraceIsEnabled)162{163this.Trace.Write("aspx.page","End ProcessPostData");164}165}166if(context.TraceIsEnabled)167{168this.Trace.Write("aspx.page","Begin PreLoad");169}170this.OnPreLoad(EventArgs.Empty);171if(context.TraceIsEnabled)172{173this.Trace.Write("aspx.page","End PreLoad");174}175if(context.TraceIsEnabled)176{177this.Trace.Write("aspx.page","Begin Load");178}179if(EtwTrace.IsTraceEnabled(5,4))180{181EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER,this._context.WorkerRequest);182}183this.LoadRecursive();184if(EtwTrace.IsTraceEnabled(5,4))185{186EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE,this._context.WorkerRequest);187}188if(context.TraceIsEnabled)189{190this.Trace.Write("aspx.page","End Load");191}192if(this.IsPostBack)193{194if(context.TraceIsEnabled)195{196this.Trace.Write("aspx.page","Begin ProcessPostData Second Try");197}198this.ProcessPostData(this._leftoverPostData,false);199if(context.TraceIsEnabled)200{201this.Trace.Write("aspx.page","End ProcessPostData Second Try");202this.Trace.Write("aspx.page","Begin Raise ChangedEvents");203}204if(EtwTrace.IsTraceEnabled(5,4))205{206EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER,this._context.WorkerRequest);207}208this.RaiseChangedEvents();209if(EtwTrace.IsTraceEnabled(5,4))210{211EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE,this._context.WorkerRequest);212}213if(context.TraceIsEnabled)214{215this.Trace.Write("aspx.page","End Raise ChangedEvents");216this.Trace.Write("aspx.page","Begin Raise PostBackEvent");217}218if(EtwTrace.IsTraceEnabled(5,4))219{220EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER,this._context.WorkerRequest);221}222this.RaisePostBackEvent(this._requestValueCollection);223if(EtwTrace.IsTraceEnabled(5,4))224{225EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE,this._context.WorkerRequest);226}227if(context.TraceIsEnabled)228{229this.Trace.Write("aspx.page","End Raise PostBackEvent");230}231}232if(context.TraceIsEnabled)233{234this.Trace.Write("aspx.page","Begin LoadComplete");235}236this.OnLoadComplete(EventArgs.Empty);237if(context.TraceIsEnabled)238{239this.Trace.Write("aspx.page","End LoadComplete");240}241if(this.IsPostBack &&this.IsCallback)242{243this.PrepareCallback(callbackControlID);244}245elseif(!this.IsCrossPagePostBack)246{247if(context.TraceIsEnabled)248{249this.Trace.Write("aspx.page","Begin PreRender");250}251if(EtwTrace.IsTraceEnabled(5,4))252{253EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER,this._context.WorkerRequest);254}255this.PreRenderRecursiveInternal();256if(EtwTrace.IsTraceEnabled(5,4))257{258EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE,this._context.WorkerRequest);259}260if(context.TraceIsEnabled)261{262this.Trace.Write("aspx.page","End PreRender");263}264}265}266if((this._legacyAsyncInfo ==null) ||this._legacyAsyncInfo.CallerIsBlocking)267{268this.ExecuteRegisteredAsyncTasks();269}270this.ValidateRawUrlIfRequired();271if(includeStagesAfterAsyncPoint)272{273if(this.IsCallback)274{275this.RenderCallback();276}277elseif(!this.IsCrossPagePostBack)278{279if(context.TraceIsEnabled)280{281this.Trace.Write("aspx.page","Begin PreRenderComplete");282}283this.PerformPreRenderComplete();284if(context.TraceIsEnabled)285{286this.Trace.Write("aspx.page","End PreRenderComplete");287}288if(context.TraceIsEnabled)289{290this.BuildPageProfileTree(this.EnableViewState);291this.Trace.Write("aspx.page","Begin SaveState");292}293if(EtwTrace.IsTraceEnabled(5,4))294{295EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER,this._context.WorkerRequest);296}297this.SaveAllState();298if(EtwTrace.IsTraceEnabled(5,4))299{300EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE,this._context.WorkerRequest);301}302if(context.TraceIsEnabled)303{304this.Trace.Write("aspx.page","End SaveState");305this.Trace.Write("aspx.page","Begin SaveStateComplete");306}307this.OnSaveStateComplete(EventArgs.Empty);308if(context.TraceIsEnabled)309{310this.Trace.Write("aspx.page","End SaveStateComplete");311this.Trace.Write("aspx.page","Begin Render");312}313if(EtwTrace.IsTraceEnabled(5,4))314{315EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER,this._context.WorkerRequest);316}317if(str !=null)318{319this.ExportWebPart(str);320}321else322{323this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));324}325if(EtwTrace.IsTraceEnabled(5,4))326{327EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE,this._context.WorkerRequest);328}329if(context.TraceIsEnabled)330{331this.Trace.Write("aspx.page","End Render");332}333this.CheckRemainingAsyncTasks(false);334}335}336}337catch(ThreadAbortException exception1)338{339HttpApplication.CancelModuleException exceptionState = exception1.ExceptionStateasHttpApplication.CancelModuleException;340if((((includeStagesBeforeAsyncPoint & includeStagesAfterAsyncPoint) && (this._context.Handler ==this)) && ((this._context.ApplicationInstance !=null) && (exceptionState !=null))) && !exceptionState.Timeout)341{342this._context.ApplicationInstance.CompleteRequest();343ThreadResetAbortWithAssert();344}345else346{347this.CheckRemainingAsyncTasks(true);348throw;349}350}351catch(ConfigurationException)352{353throw;354}355catch(Exception exception2)356{357PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_DURING_REQUEST);358PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL);359if(!this.HandleError(exception2))360{361throw;362}363}364}365366367368369privatevoidProcessRequestMain(boolincludeStagesBeforeAsyncPoint,boolincludeStagesAfterAsyncPoint)370{371try372{373HttpContext context =this.Context;374stringstr =null;375if(includeStagesBeforeAsyncPoint)376{377if(this.IsInAspCompatMode)378{379AspCompatApplicationStep.OnPageStartSessionObjects();380}381if(this.PageAdapter !=null)382{383this._requestValueCollection =this.PageAdapter.DeterminePostBackMode();384if(this._requestValueCollection !=null)385{386this._unvalidatedRequestValueCollection =this.PageAdapter.DeterminePostBackModeUnvalidated();387}388}389else390{391this._requestValueCollection =this.DeterminePostBackMode();392if(this._requestValueCollection !=null)393{394this._unvalidatedRequestValueCollection =this.DeterminePostBackModeUnvalidated();395}396}397stringcallbackControlID =string.Empty;398if(this.DetermineIsExportingWebPart())399{400if(!RuntimeConfig.GetAppConfig().WebParts.EnableExport)401{402thrownewInvalidOperationException(SR.GetString("WebPartExportHandler_DisabledExportHandler"));403}404str =this.Request.QueryString["webPart"];405if(string.IsNullOrEmpty(str))406{407thrownewInvalidOperationException(SR.GetString("WebPartExportHandler_InvalidArgument"));408}409if(string.Equals(this.Request.QueryString["scope"],"shared", StringComparison.OrdinalIgnoreCase))410{411this._pageFlags.Set(4);412}413stringstr3 =this.Request.QueryString["query"];414if(str3 ==null)415{416str3 =string.Empty;417}418this.Request.QueryStringText =str3;419context.Trace.IsEnabled =false;420}421if(this._requestValueCollection !=null)422{423if(this._requestValueCollection["__VIEWSTATEENCRYPTED"] !=null)424{425this.ContainsEncryptedViewState =true;426}427callbackControlID =this._requestValueCollection["__CALLBACKID"];428if((callbackControlID !=null) && (this._request.HttpVerb ==HttpVerb.POST))429{430this._isCallback =true;431}432elseif(!this.IsCrossPagePostBack)433{434VirtualPath path =null;435if(this._requestValueCollection["__PREVIOUSPAGE"] !=null)436{437try438{439path = VirtualPath.CreateNonRelativeAllowNull(DecryptString(this._requestValueCollection["__PREVIOUSPAGE"], Purpose.WebForms_Page_PreviousPageID));440}441catch442{443this._pageFlags[8] =true;444}445if((path !=null) && (path !=this.Request.CurrentExecutionFilePathObject))446{447this._pageFlags[8] =true;448this._previousPagePath =path;449}450}451}452}453if(this.MaintainScrollPositionOnPostBack)454{455this.LoadScrollPosition();456}457if(context.TraceIsEnabled)458{459this.Trace.Write("aspx.page","Begin PreInit");460}461if(EtwTrace.IsTraceEnabled(5,4))462{463EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER,this._context.WorkerRequest);464}465this.PerformPreInit();466if(EtwTrace.IsTraceEnabled(5,4))467{468EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE,this._context.WorkerRequest);469}470if(context.TraceIsEnabled)471{472this.Trace.Write("aspx.page","End PreInit");473}474if(context.TraceIsEnabled)475{476this.Trace.Write("aspx.page","Begin Init");477}478if(EtwTrace.IsTraceEnabled(5,4))479{480EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER,this._context.WorkerRequest);481}482this.InitRecursive(null);483if(EtwTrace.IsTraceEnabled(5,4))484{485EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE,this._context.WorkerRequest);486}487if(context.TraceIsEnabled)488{489this.Trace.Write("aspx.page","End Init");490}491if(context.TraceIsEnabled)492{493this.Trace.Write("aspx.page","Begin InitComplete");494}495this.OnInitComplete(EventArgs.Empty);496if(context.TraceIsEnabled)497{498this.Trace.Write("aspx.page","End InitComplete");499}500if(this.IsPostBack)501{502if(context.TraceIsEnabled)503{504this.Trace.Write("aspx.page","Begin LoadState");505}506if(EtwTrace.IsTraceEnabled(5,4))507{508EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER,this._context.WorkerRequest);509}510this.LoadAllState();511if(EtwTrace.IsTraceEnabled(5,4))512{513EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE,this._context.WorkerRequest);514}515if(context.TraceIsEnabled)516{517this.Trace.Write("aspx.page","End LoadState");518this.Trace.Write("aspx.page","Begin ProcessPostData");519}520if(EtwTrace.IsTraceEnabled(5,4))521{522EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER,this._context.WorkerRequest);523}524this.ProcessPostData(this._requestValueCollection,true);525if(EtwTrace.IsTraceEnabled(5,4))526{527EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE,this._context.WorkerRequest);528}529if(context.TraceIsEnabled)530{531this.Trace.Write("aspx.page","End ProcessPostData");532}533}534if(context.TraceIsEnabled)535{536this.Trace.Write("aspx.page","Begin PreLoad");537}538this.OnPreLoad(EventArgs.Empty);539if(context.TraceIsEnabled)540{541this.Trace.Write("aspx.page","End PreLoad");542}543if(context.TraceIsEnabled)544{545this.Trace.Write("aspx.page","Begin Load");546}547if(EtwTrace.IsTraceEnabled(5,4))548{549EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER,this._context.WorkerRequest);550}551this.LoadRecursive();552if(EtwTrace.IsTraceEnabled(5,4))553{554EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE,this._context.WorkerRequest);555}556if(context.TraceIsEnabled)557{558this.Trace.Write("aspx.page","End Load");559}560if(this.IsPostBack)561{562if(context.TraceIsEnabled)563{564this.Trace.Write("aspx.page","Begin ProcessPostData Second Try");565}566this.ProcessPostData(this._leftoverPostData,false);567if(context.TraceIsEnabled)568{569this.Trace.Write("aspx.page","End ProcessPostData Second Try");570this.Trace.Write("aspx.page","Begin Raise ChangedEvents");571}572if(EtwTrace.IsTraceEnabled(5,4))573{574EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER,this._context.WorkerRequest);575}576this.RaiseChangedEvents();577if(EtwTrace.IsTraceEnabled(5,4))578{579EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE,this._context.WorkerRequest);580}581if(context.TraceIsEnabled)582{583this.Trace.Write("aspx.page","End Raise ChangedEvents");584this.Trace.Write("aspx.page","Begin Raise PostBackEvent");585}586if(EtwTrace.IsTraceEnabled(5,4))587{588EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER,this._context.WorkerRequest);589}590this.RaisePostBackEvent(this._requestValueCollection);591if(EtwTrace.IsTraceEnabled(5,4))592{593EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE,this._context.WorkerRequest);594}595if(context.TraceIsEnabled)596{597this.Trace.Write("aspx.page","End Raise PostBackEvent");598}599}600if(context.TraceIsEnabled)601{602this.Trace.Write("aspx.page","Begin LoadComplete");603}604this.OnLoadComplete(EventArgs.Empty);605if(context.TraceIsEnabled)606{607this.Trace.Write("aspx.page","End LoadComplete");608}609if(this.IsPostBack &&this.IsCallback)610{611this.PrepareCallback(callbackControlID);612}613elseif(!this.IsCrossPagePostBack)614{615if(context.TraceIsEnabled)616{617this.Trace.Write("aspx.page","Begin PreRender");618}619if(EtwTrace.IsTraceEnabled(5,4))620{621EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER,this._context.WorkerRequest);622}623this.PreRenderRecursiveInternal();624if(EtwTrace.IsTraceEnabled(5,4))625{626EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE,this._context.WorkerRequest);627}628if(context.TraceIsEnabled)629{630this.Trace.Write("aspx.page","End PreRender");631}632}633}634if((this._legacyAsyncInfo ==null) ||this._legacyAsyncInfo.CallerIsBlocking)635{636this.ExecuteRegisteredAsyncTasks();637}638this.ValidateRawUrlIfRequired();639if(includeStagesAfterAsyncPoint)640{641if(this.IsCallback)642{643this.RenderCallback();644}645elseif(!this.IsCrossPagePostBack)646{647if(context.TraceIsEnabled)648{649this.Trace.Write("aspx.page","Begin PreRenderComplete");650}651this.PerformPreRenderComplete();652if(context.TraceIsEnabled)653{654this.Trace.Write("aspx.page","End PreRenderComplete");655}656if(context.TraceIsEnabled)657{658this.BuildPageProfileTree(this.EnableViewState);659this.Trace.Write("aspx.page","Begin SaveState");660}661if(EtwTrace.IsTraceEnabled(5,4))662{663EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER,this._context.WorkerRequest);664}665this.SaveAllState();666if(EtwTrace.IsTraceEnabled(5,4))667{668EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE,this._context.WorkerRequest);669}670if(context.TraceIsEnabled)671{672this.Trace.Write("aspx.page","End SaveState");673this.Trace.Write("aspx.page","Begin SaveStateComplete");674}675this.OnSaveStateComplete(EventArgs.Empty);676if(context.TraceIsEnabled)677{678this.Trace.Write("aspx.page","End SaveStateComplete");679this.Trace.Write("aspx.page","Begin Render");680}681if(EtwTrace.IsTraceEnabled(5,4))682{683EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER,this._context.WorkerRequest);684}685if(str !=null)686{687this.ExportWebPart(str);688}689else690{691this.RenderControl(this.CreateHtmlTextWriter(this.Response.Output));692}693if(EtwTrace.IsTraceEnabled(5,4))694{695EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE,this._context.WorkerRequest);696}697if(context.TraceIsEnabled)698{699this.Trace.Write("aspx.page","End Render");700}701this.CheckRemainingAsyncTasks(false);702}703}704}705catch(ThreadAbortException exception1)706{707HttpApplication.CancelModuleException exceptionState = exception1.ExceptionStateasHttpApplication.CancelModuleException;708if((((includeStagesBeforeAsyncPoint & includeStagesAfterAsyncPoint) && (this._context.Handler ==this)) && ((this._context.ApplicationInstance !=null) && (exceptionState !=null))) && !exceptionState.Timeout)709{710this._context.ApplicationInstance.CompleteRequest();711ThreadResetAbortWithAssert();712}713else714{715this.CheckRemainingAsyncTasks(true);716throw;717}718}719catch(ConfigurationException)720{721throw;722}723catch(Exception exception2)724{725PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_DURING_REQUEST);726PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL);727if(!this.HandleError(exception2))728{729throw;730}731}732}733734

View Code

如果觉得本文对你有所帮助不妨点一下【推荐】! 如果文中有不妥或者错误的地方还望指出,以免误人子弟。

作者:张一辉

出处:http://www.cnblogs.com/zhangyihui/

本文以学习、研究和分享为主,版权归作者和博客园共有,欢迎转载,但必须在文章页面明显位置给出此段申明。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航