欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解ASP.NET页面生命周期

程序员文章站 2023-12-20 19:16:10
asp.net页面运行时候,页面将经历一个生命周期,在生命周期中将执行一系列的处理步骤。包括初始化、实例化控件、还原和维护状态、运行时间处理程序代码以及进行呈现。熟悉页面生...

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应用程序周期中prerequesthandlerexecute事件与postrequesthandlerexecute事件之间就是我们的页面生命周期了,对于aspx页面就是一系列的打造页面控件树,触发各种页面时间,对于一般处理程序ashx就是直接执行咱们开发者写的processrequest方法了,对于mvc应用程序就是创建控制器工厂,创建控制器对象,调用action那一套了。

下面主要讲述的就是asp.net webform中的页面的生命周期了。

详解ASP.NET页面生命周期

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

1.打造页面控件树

frameworkinitialize内部调用了_buildcontroltree()方法


详解ASP.NET页面生命周期
  

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

下面看一张原理图


详解ASP.NET页面生命周期

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

2.触发performpreinit事件 

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

private void performpreinit()

{

 this.onpreinit(eventargs.empty);

 this.initializethemes();

 this.applymasterpage();

 this._preinitworkcomplete = true;

}

3.触发initrecursive事件

详解ASP.NET页面生命周期

4.触发loadallstate()事件

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

5.触发processpostdata(this._requestvaluecollection, true)事件

private void processpostdata(namevaluecollection postdata, bool fbeforeload)
{
 if (this._changedpostdataconsumers == null)
 {
 this._changedpostdataconsumers = new arraylist();
 }
 if (postdata != null)
 {
 foreach (string str in postdata)
 {
  if ((str != null) && !issystempostfield(str))
  {
  control control = this.findcontrol(str);
  if (control == null)
  {
   if (fbeforeload)
   {
   if (this._leftoverpostdata == null)
   {
    this._leftoverpostdata = new namevaluecollection();
   }
   this._leftoverpostdata.add(str, null);
   }
  }
  else
  {
   ipostbackdatahandler postbackdatahandler = control.postbackdatahandler;
   if (postbackdatahandler == null)
   {
   if (control.postbackeventhandler != null)
   {
    this.registerrequiresraiseevent(control.postbackeventhandler);
   }
   }
   else
   {
   if (postbackdatahandler != null)
   {
    namevaluecollection postcollection = control.calculateeffectivevalidaterequest() ? this._requestvaluecollection : this._unvalidatedrequestvaluecollection;
    if (postbackdatahandler.loadpostdata(str, postcollection))
    {
    this._changedpostdataconsumers.add(control);
    }
   }
   if (this._controlsrequiringpostback != null)
   {
    this._controlsrequiringpostback.remove(str);
   }
   }
  }
  }
 }
 }
 arraylist list = null;
 if (this._controlsrequiringpostback != null)
 {
 foreach (string str2 in this._controlsrequiringpostback)
 {
  control control2 = this.findcontrol(str2);
  if (control2 != null)
  {
  ipostbackdatahandler adapterinternal = control2.adapterinternal as ipostbackdatahandler;
  if (adapterinternal == null)
  {
   adapterinternal = control2 as ipostbackdatahandler;
  }
  if (adapterinternal == null)
  {
   object[] args = new object[] { str2 };
   throw new httpexception(sr.getstring("postback_ctrl_not_found", args));
  }
  namevaluecollection values2 = control2.calculateeffectivevalidaterequest() ? this._requestvaluecollection : this._unvalidatedrequestvaluecollection;
  if (adapterinternal.loadpostdata(str2, values2))
  {
   this._changedpostdataconsumers.add(control2);
  }
  }
  else if (fbeforeload)
  {
  if (list == null)
  {
   list = new arraylist();
  }
  list.add(str2);
  }
 }
 this._controlsrequiringpostback = list;
 }
}

主要做了两件事

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

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

6.触发loadrecursive()事件

详解ASP.NET页面生命周期

大名鼎鼎的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事件

详解ASP.NET页面生命周期

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

9.触发raisepostbackevent(this._requestvaluecollection)事件

详解ASP.NET页面生命周期

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

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

11.触发事件saveallstate事件

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

12.rendercontrol(this.createhtmltextwriter(this.response.output))

把要发送到客户端浏览器的内容设置到response.output,应用程序将它发送 到客户端浏览器。[/code]

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

最后附上生命周期processrequest源码  

private void processrequestmain(bool includestagesbeforeasyncpoint, bool includestagesafterasyncpoint)
{
 try
 {
 httpcontext context = this.context;
 string str = null;
 if (includestagesbeforeasyncpoint)
 {
  if (this.isinaspcompatmode)
  {
  aspcompatapplicationstep.onpagestartsessionobjects();
  }
  if (this.pageadapter != null)
  {
  this._requestvaluecollection = this.pageadapter.determinepostbackmode();
  if (this._requestvaluecollection != null)
  {
   this._unvalidatedrequestvaluecollection = this.pageadapter.determinepostbackmodeunvalidated();
  }
  }
  else
  {
  this._requestvaluecollection = this.determinepostbackmode();
  if (this._requestvaluecollection != null)
  {
   this._unvalidatedrequestvaluecollection = this.determinepostbackmodeunvalidated();
  }
  }
  string callbackcontrolid = string.empty;
  if (this.determineisexportingwebpart())
  {
  if (!runtimeconfig.getappconfig().webparts.enableexport)
  {
   throw new invalidoperationexception(sr.getstring("webpartexporthandler_disabledexporthandler"));
  }
  str = this.request.querystring["webpart"];
  if (string.isnullorempty(str))
  {
   throw new invalidoperationexception(sr.getstring("webpartexporthandler_invalidargument"));
  }
  if (string.equals(this.request.querystring["scope"], "shared", stringcomparison.ordinalignorecase))
  {
   this._pageflags.set(4);
  }
  string str3 = this.request.querystring["query"];
  if (str3 == null)
  {
   str3 = string.empty;
  }
  this.request.querystringtext = str3;
  context.trace.isenabled = false;
  }
  if (this._requestvaluecollection != null)
  {
  if (this._requestvaluecollection["__viewstateencrypted"] != null)
  {
   this.containsencryptedviewstate = true;
  }
  callbackcontrolid = this._requestvaluecollection["__callbackid"];
  if ((callbackcontrolid != null) && (this._request.httpverb == httpverb.post))
  {
   this._iscallback = true;
  }
  else if (!this.iscrosspagepostback)
  {
   virtualpath path = null;
   if (this._requestvaluecollection["__previouspage"] != null)
   {
   try
   {
    path = virtualpath.createnonrelativeallownull(decryptstring(this._requestvaluecollection["__previouspage"], purpose.webforms_page_previouspageid));
   }
   catch
   {
    this._pageflags[8] = true;
   }
   if ((path != null) && (path != this.request.currentexecutionfilepathobject))
   {
    this._pageflags[8] = true;
    this._previouspagepath = path;
   }
   }
  }
  }
  if (this.maintainscrollpositiononpostback)
  {
  this.loadscrollposition();
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin preinit");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_pre_init_enter, this._context.workerrequest);
  }
  this.performpreinit();
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_pre_init_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end preinit");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin init");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_init_enter, this._context.workerrequest);
  }
  this.initrecursive(null);
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_init_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end init");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin initcomplete");
  }
  this.oninitcomplete(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end initcomplete");
  }
  if (this.ispostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin loadstate");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_viewstate_enter, this._context.workerrequest);
  }
  this.loadallstate();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_viewstate_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end loadstate");
   this.trace.write("aspx.page", "begin processpostdata");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_postdata_enter, this._context.workerrequest);
  }
  this.processpostdata(this._requestvaluecollection, true);
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_postdata_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end processpostdata");
  }
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin preload");
  }
  this.onpreload(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end preload");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin load");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_load_enter, this._context.workerrequest);
  }
  this.loadrecursive();
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_load_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end load");
  }
  if (this.ispostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin processpostdata second try");
  }
  this.processpostdata(this._leftoverpostdata, false);
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end processpostdata second try");
   this.trace.write("aspx.page", "begin raise changedevents");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_post_data_changed_enter, this._context.workerrequest);
  }
  this.raisechangedevents();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_post_data_changed_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end raise changedevents");
   this.trace.write("aspx.page", "begin raise postbackevent");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_raise_postback_enter, this._context.workerrequest);
  }
  this.raisepostbackevent(this._requestvaluecollection);
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_raise_postback_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end raise postbackevent");
  }
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin loadcomplete");
  }
  this.onloadcomplete(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end loadcomplete");
  }
  if (this.ispostback && this.iscallback)
  {
  this.preparecallback(callbackcontrolid);
  }
  else if (!this.iscrosspagepostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin prerender");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_pre_render_enter, this._context.workerrequest);
  }
  this.prerenderrecursiveinternal();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_pre_render_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end prerender");
  }
  }
 }
 if ((this._legacyasyncinfo == null) || this._legacyasyncinfo.callerisblocking)
 {
  this.executeregisteredasynctasks();
 }
 this.validaterawurlifrequired();
 if (includestagesafterasyncpoint)
 {
  if (this.iscallback)
  {
  this.rendercallback();
  }
  else if (!this.iscrosspagepostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin prerendercomplete");
  }
  this.performprerendercomplete();
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end prerendercomplete");
  }
  if (context.traceisenabled)
  {
   this.buildpageprofiletree(this.enableviewstate);
   this.trace.write("aspx.page", "begin savestate");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_save_viewstate_enter, this._context.workerrequest);
  }
  this.saveallstate();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_save_viewstate_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end savestate");
   this.trace.write("aspx.page", "begin savestatecomplete");
  }
  this.onsavestatecomplete(eventargs.empty);
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end savestatecomplete");
   this.trace.write("aspx.page", "begin render");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_render_enter, this._context.workerrequest);
  }
  if (str != null)
  {
   this.exportwebpart(str);
  }
  else
  {
   this.rendercontrol(this.createhtmltextwriter(this.response.output));
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_render_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end render");
  }
  this.checkremainingasynctasks(false);
  }
 }
 }
 catch (threadabortexception exception1)
 {
 httpapplication.cancelmoduleexception exceptionstate = exception1.exceptionstate as httpapplication.cancelmoduleexception;
 if ((((includestagesbeforeasyncpoint & includestagesafterasyncpoint) && (this._context.handler == this)) && ((this._context.applicationinstance != null) && (exceptionstate != null))) && !exceptionstate.timeout)
 {
  this._context.applicationinstance.completerequest();
  threadresetabortwithassert();
 }
 else
 {
  this.checkremainingasynctasks(true);
  throw;
 }
 }
 catch (configurationexception)
 {
 throw;
 }
 catch (exception exception2)
 {
 perfcounters.incrementcounter(appperfcounter.errors_during_request);
 perfcounters.incrementcounter(appperfcounter.errors_total);
 if (!this.handleerror(exception2))
 {
  throw;
 }
 }
}

 

 
 private void processrequestmain(bool includestagesbeforeasyncpoint, bool includestagesafterasyncpoint)
{
 try
 {
 httpcontext context = this.context;
 string str = null;
 if (includestagesbeforeasyncpoint)
 {
  if (this.isinaspcompatmode)
  {
  aspcompatapplicationstep.onpagestartsessionobjects();
  }
  if (this.pageadapter != null)
  {
  this._requestvaluecollection = this.pageadapter.determinepostbackmode();
  if (this._requestvaluecollection != null)
  {
   this._unvalidatedrequestvaluecollection = this.pageadapter.determinepostbackmodeunvalidated();
  }
  }
  else
  {
  this._requestvaluecollection = this.determinepostbackmode();
  if (this._requestvaluecollection != null)
  {
   this._unvalidatedrequestvaluecollection = this.determinepostbackmodeunvalidated();
  }
  }
  string callbackcontrolid = string.empty;
  if (this.determineisexportingwebpart())
  {
  if (!runtimeconfig.getappconfig().webparts.enableexport)
  {
   throw new invalidoperationexception(sr.getstring("webpartexporthandler_disabledexporthandler"));
  }
  str = this.request.querystring["webpart"];
  if (string.isnullorempty(str))
  {
   throw new invalidoperationexception(sr.getstring("webpartexporthandler_invalidargument"));
  }
  if (string.equals(this.request.querystring["scope"], "shared", stringcomparison.ordinalignorecase))
  {
   this._pageflags.set(4);
  }
  string str3 = this.request.querystring["query"];
  if (str3 == null)
  {
   str3 = string.empty;
  }
  this.request.querystringtext = str3;
  context.trace.isenabled = false;
  }
  if (this._requestvaluecollection != null)
  {
  if (this._requestvaluecollection["__viewstateencrypted"] != null)
  {
   this.containsencryptedviewstate = true;
  }
  callbackcontrolid = this._requestvaluecollection["__callbackid"];
  if ((callbackcontrolid != null) && (this._request.httpverb == httpverb.post))
  {
   this._iscallback = true;
  }
  else if (!this.iscrosspagepostback)
  {
   virtualpath path = null;
   if (this._requestvaluecollection["__previouspage"] != null)
   {
   try
   {
    path = virtualpath.createnonrelativeallownull(decryptstring(this._requestvaluecollection["__previouspage"], purpose.webforms_page_previouspageid));
   }
   catch
   {
    this._pageflags[8] = true;
   }
   if ((path != null) && (path != this.request.currentexecutionfilepathobject))
   {
    this._pageflags[8] = true;
    this._previouspagepath = path;
   }
   }
  }
  }
  if (this.maintainscrollpositiononpostback)
  {
  this.loadscrollposition();
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin preinit");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_pre_init_enter, this._context.workerrequest);
  }
  this.performpreinit();
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_pre_init_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end preinit");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin init");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_init_enter, this._context.workerrequest);
  }
  this.initrecursive(null);
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_init_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end init");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin initcomplete");
  }
  this.oninitcomplete(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end initcomplete");
  }
  if (this.ispostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin loadstate");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_viewstate_enter, this._context.workerrequest);
  }
  this.loadallstate();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_viewstate_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end loadstate");
   this.trace.write("aspx.page", "begin processpostdata");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_postdata_enter, this._context.workerrequest);
  }
  this.processpostdata(this._requestvaluecollection, true);
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_load_postdata_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end processpostdata");
  }
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin preload");
  }
  this.onpreload(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end preload");
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin load");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_load_enter, this._context.workerrequest);
  }
  this.loadrecursive();
  if (etwtrace.istraceenabled(5, 4))
  {
  etwtrace.trace(etwtracetype.etw_type_page_load_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end load");
  }
  if (this.ispostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin processpostdata second try");
  }
  this.processpostdata(this._leftoverpostdata, false);
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end processpostdata second try");
   this.trace.write("aspx.page", "begin raise changedevents");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_post_data_changed_enter, this._context.workerrequest);
  }
  this.raisechangedevents();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_post_data_changed_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end raise changedevents");
   this.trace.write("aspx.page", "begin raise postbackevent");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_raise_postback_enter, this._context.workerrequest);
  }
  this.raisepostbackevent(this._requestvaluecollection);
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_raise_postback_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end raise postbackevent");
  }
  }
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "begin loadcomplete");
  }
  this.onloadcomplete(eventargs.empty);
  if (context.traceisenabled)
  {
  this.trace.write("aspx.page", "end loadcomplete");
  }
  if (this.ispostback && this.iscallback)
  {
  this.preparecallback(callbackcontrolid);
  }
  else if (!this.iscrosspagepostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin prerender");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_pre_render_enter, this._context.workerrequest);
  }
  this.prerenderrecursiveinternal();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_pre_render_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end prerender");
  }
  }
 }
 if ((this._legacyasyncinfo == null) || this._legacyasyncinfo.callerisblocking)
 {
  this.executeregisteredasynctasks();
 }
 this.validaterawurlifrequired();
 if (includestagesafterasyncpoint)
 {
  if (this.iscallback)
  {
  this.rendercallback();
  }
  else if (!this.iscrosspagepostback)
  {
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "begin prerendercomplete");
  }
  this.performprerendercomplete();
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end prerendercomplete");
  }
  if (context.traceisenabled)
  {
   this.buildpageprofiletree(this.enableviewstate);
   this.trace.write("aspx.page", "begin savestate");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_save_viewstate_enter, this._context.workerrequest);
  }
  this.saveallstate();
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_save_viewstate_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end savestate");
   this.trace.write("aspx.page", "begin savestatecomplete");
  }
  this.onsavestatecomplete(eventargs.empty);
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end savestatecomplete");
   this.trace.write("aspx.page", "begin render");
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_render_enter, this._context.workerrequest);
  }
  if (str != null)
  {
   this.exportwebpart(str);
  }
  else
  {
   this.rendercontrol(this.createhtmltextwriter(this.response.output));
  }
  if (etwtrace.istraceenabled(5, 4))
  {
   etwtrace.trace(etwtracetype.etw_type_page_render_leave, this._context.workerrequest);
  }
  if (context.traceisenabled)
  {
   this.trace.write("aspx.page", "end render");
  }
  this.checkremainingasynctasks(false);
  }
 }
 }
 catch (threadabortexception exception1)
 {
 httpapplication.cancelmoduleexception exceptionstate = exception1.exceptionstate as httpapplication.cancelmoduleexception;
 if ((((includestagesbeforeasyncpoint & includestagesafterasyncpoint) && (this._context.handler == this)) && ((this._context.applicationinstance != null) && (exceptionstate != null))) && !exceptionstate.timeout)
 {
  this._context.applicationinstance.completerequest();
  threadresetabortwithassert();
 }
 else
 {
  this.checkremainingasynctasks(true);
  throw;
 }
 }
 catch (configurationexception)
 {
 throw;
 }
 catch (exception exception2)
 {
 perfcounters.incrementcounter(appperfcounter.errors_during_request);
 perfcounters.incrementcounter(appperfcounter.errors_total);
 if (!this.handleerror(exception2))
 {
  throw;
 }
 }
}

以上就是关于asp.net页面生命周期的详细内容介绍,希望对大家的学习有所帮助。

上一篇:

下一篇: