你的位置:首页 > ASP.net教程

[ASP.net教程]【深入ASP.NET原理系列】


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

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

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

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

  2.Page_LoadResponse.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中的页面的生命周期了。

  

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

  一、打造页面控件数

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

  二、触发PerformPreInit事件 

    

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

    

 1 private void PerformPreInit() 2  3 { 4  5   this.OnPreInit(EventArgs.Empty); 6  7   this.InitializeThemes(); 8  9   this.ApplyMasterPage();10 11   this._preInitWorkComplete = true;12 13 }

View Code

 

     

  三、触发InitRecursive事件

    

  

  四、触发LoadAllState()事件

        加载页面状态解析ViewState,将页面表单中的ViewState进行反Base64编码,反序列化,存在页面的ViewState属性中

    

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

      

 1 private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad) 2 { 3   if (this._changedPostDataConsumers == null) 4   { 5     this._changedPostDataConsumers = new ArrayList(); 6   } 7   if (postData != null) 8   { 9     foreach (string str in postData) 10     { 11       if ((str != null) && !IsSystemPostField(str)) 12       { 13         Control control = this.FindControl(str); 14         if (control == null) 15         { 16           if (fBeforeLoad) 17           { 18             if (this._leftoverPostData == null) 19             { 20               this._leftoverPostData = new NameValueCollection(); 21             } 22             this._leftoverPostData.Add(str, null); 23           } 24         } 25         else 26         { 27           IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler; 28           if (postBackDataHandler == null) 29           { 30             if (control.PostBackEventHandler != null) 31             { 32               this.RegisterRequiresRaiseEvent(control.PostBackEventHandler); 33             } 34           } 35           else 36           { 37             if (postBackDataHandler != null) 38             { 39               NameValueCollection postCollection = control.CalculateEffectiveValidateRequest() ? this._requestValueCollection : this._unvalidatedRequestValueCollection; 40               if (postBackDataHandler.LoadPostData(str, postCollection)) 41               { 42                 this._changedPostDataConsumers.Add(control); 43               } 44             } 45             if (this._controlsRequiringPostBack != null) 46             { 47               this._controlsRequiringPostBack.Remove(str); 48             } 49           } 50         } 51       } 52     } 53   } 54   ArrayList list = null; 55   if (this._controlsRequiringPostBack != null) 56   { 57     foreach (string str2 in this._controlsRequiringPostBack) 58     { 59       Control control2 = this.FindControl(str2); 60       if (control2 != null) 61       { 62         IPostBackDataHandler adapterInternal = control2.AdapterInternal as IPostBackDataHandler; 63         if (adapterInternal == null) 64         { 65           adapterInternal = control2 as IPostBackDataHandler; 66         } 67         if (adapterInternal == null) 68         { 69           object[] args = new object[] { str2 }; 70           throw new HttpException(SR.GetString("Postback_ctrl_not_found", args)); 71         } 72         NameValueCollection values2 = control2.CalculateEffectiveValidateRequest() ? this._requestValueCollection : this._unvalidatedRequestValueCollection; 73         if (adapterInternal.LoadPostData(str2, values2)) 74         { 75           this._changedPostDataConsumers.Add(control2); 76         } 77       } 78       else if (fBeforeLoad) 79       { 80         if (list == null) 81         { 82           list = new ArrayList(); 83         } 84         list.Add(str2); 85       } 86     } 87     this._controlsRequiringPostBack = list; 88   } 89 } 90  91  92  93  94  private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad) 95 { 96   if (this._changedPostDataConsumers == null) 97   { 98     this._changedPostDataConsumers = new ArrayList(); 99   }100   if (postData != null)101   {102     foreach (string str in postData)103     {104       if ((str != null) && !IsSystemPostField(str))105       {106         Control control = this.FindControl(str);107         if (control == null)108         {109           if (fBeforeLoad)110           {111             if (this._leftoverPostData == null)112             {113               this._leftoverPostData = new NameValueCollection();114             }115             this._leftoverPostData.Add(str, null);116           }117         }118         else119         {120           IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler;121           if (postBackDataHandler == null)122           {123             if (control.PostBackEventHandler != null)124             {125               this.RegisterRequiresRaiseEvent(control.PostBackEventHandler);126             }127           }128           else129           {130             if (postBackDataHandler != null)131             {132               NameValueCollection postCollection = control.CalculateEffectiveValidateRequest() ? this._requestValueCollection : this._unvalidatedRequestValueCollection;133               if (postBackDataHandler.LoadPostData(str, postCollection))134               {135                 this._changedPostDataConsumers.Add(control);136               }137             }138             if (this._controlsRequiringPostBack != null)139             {140               this._controlsRequiringPostBack.Remove(str);141             }142           }143         }144       }145     }146   }147   ArrayList list = null;148   if (this._controlsRequiringPostBack != null)149   {150     foreach (string str2 in this._controlsRequiringPostBack)151     {152       Control control2 = this.FindControl(str2);153       if (control2 != null)154       {155         IPostBackDataHandler adapterInternal = control2.AdapterInternal as IPostBackDataHandler;156         if (adapterInternal == null)157         {158           adapterInternal = control2 as IPostBackDataHandler;159         }160         if (adapterInternal == null)161         {162           object[] args = new object[] { str2 };163           throw new HttpException(SR.GetString("Postback_ctrl_not_found", args));164         }165         NameValueCollection values2 = control2.CalculateEffectiveValidateRequest() ? this._requestValueCollection : this._unvalidatedRequestValueCollection;166         if (adapterInternal.LoadPostData(str2, values2))167         {168           this._changedPostDataConsumers.Add(control2);169         }170       }171       else if (fBeforeLoad)172       {173         if (list == null)174         {175           list = new ArrayList();176         }177         list.Add(str2);178       }179     }180     this._controlsRequiringPostBack = list;181   }182 }183 184 

View Code

 

      主要做了两件事
      1)将表单里提交过来的控件数据设置给页面对象的控件树中对应控件的属性(给前面打造的控件树里面控件给值),        这样在服务器端就可以拿到客户端输入的值了。
      2)将表单里面提交过来的值与ViewState中控件原来的值进行比对,不同则表示要触发该控件的Change 事件,则同        时将该控件放到一个集合(看源码其实就是changedPostDataConsumers)中。在后续执行过程中遍历改集合        依次触发对应控件的Change事件。

  六、触发LoadRecursive()事件

        

 

      大名鼎鼎的Page_Load就是在这里执行的。不过是先执行页面本身的Load事件再执行页面控件的Load事件哦

   七、再次触发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中手动创建的控件还原值
    

  八、触发RaiseChangedEvents事件

      

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

   九、触发RaisePostBackEvent(this._requestValueCollection)事件

      

 

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

   十、触发PerformPreRenderComplete事件

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

   十一、触发事件SaveAllState事件

  

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

  十二、RenderControl(this.CreateHtmlTextWriter(this.Response.Output))

  

     把要发送到客户端浏览器的内容设置到Response.Output,应用程序将它发送 到客户端浏览器。
    看到这里不知道大家是否已经可以清晰地回答开篇提到的几个问题了,其实就是这些事件执行的先后顺序,页面生命周期了

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

View Code

 

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

作者:张一辉

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

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