验证一个ASP.NET应用程序和页面的生命周期的实现代码

  如果我们能更好地掌握这样一个过程,那么对单个ASP.NET Page的生命周期也能更好地了解:

  下面介绍如何编写一个简单的ASP.NET 页面和一个简单的HttpModule,对MSDN里提到的ASP.NET的生命周期进行验证

  1. 首先使用Visual Studio 2010建立一个空的ASP.NET网站 (ASP.NET 4.0)

  2. 添加一个Default.aspx,添加三个ASP.NET控件,分别为TextBox,Button和Validator:

  

复制代码 代码如下:
  

  <form id="form1" runat="server">

  <div>

  <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

  <asp:Button ID="btnSubmit" runat="server" Text="OK" onclick="btnSubmit_Click" />

  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please input your name!" ControlToValidate="txtName" ForeColor="#FF3300">

  </asp:RequiredFieldValidator>

  </div>

  </form>

  3. 添加一个ASP.NEt的App_code文件夹,新建一个类,内容为:

  

复制代码 代码如下:

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Web;

  public class TestClass : IHttpModule

  {

  HttpApplication httpApp;

  public static List<string> EventList = new List<string>();

  public TestClass()

  {

  }

  public void Dispose()

  { }

  public void Init(HttpApplication context)

  {

  this.httpApp = context;

  //EventList.Clear();

  EventList.Add("Initiated");

  context.BeginRequest += new EventHandler(context_BeginRequest);

  context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);

  context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);

  context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);

  context.AcquireRequestState += new EventHandler(context_AcquireRequestState);

  context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

  context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState);

  context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);

  context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);

  context.EndRequest += new EventHandler(context_EndRequest);

  }

  private void context_EndRequest(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: End Request <hr>");

  foreach (string str in EventList)

  {

  httpApp.Response.Write(str + "<br>");

  }

  EventList.Clear();

  }

  void context_UpdateRequestCache(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Update Request Cache");

  }

  void context_ReleaseRequestState(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Release Request State");

  }

  void context_PostReleaseRequestState(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Post Release Request State");

  }

  void context_PreRequestHandlerExecute(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Pre Request Handler Execution");

  }

  void context_AcquireRequestState(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Acquire Request State");

  }

  void context_ResolveRequestCache(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Resolve Request");

  }

  void context_AuthorizeRequest(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Authorize Request");

  }

  void context_AuthenticateRequest(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: AuthenticateRequest");

  }

  void context_BeginRequest(object sender, EventArgs e)

  {

  EventList.Add("HTTP Modules: Begin Request");

  }

  }

  4. 修改刚才的Default.aspx的后台cs代码:

  

复制代码 代码如下:

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Web;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  public partial class _Default : System.Web.UI.Page

  {

  protected void Page_Init()

  {

  TestClass.EventList.Add("ASP.NET Page: Page_Init");

  }

  protected void Page_Load(object sender, EventArgs e)

  {

  TestClass.EventList.Add("ASP.NET Page: Page_Load");

  }

  public override void Validate()

  {

  TestClass.EventList.Add("ASP.NET Page: Validated");

  base.Validate();

  }

  protected void btnSubmit_Click(object sender, EventArgs e)

  {

  TestClass.EventList.Add("ASP.NET Page: Event");

  }

  protected override void Render(HtmlTextWriter writer)

  {

  TestClass.EventList.Add("ASP.NET Page: Render");

  base.Render(writer);

  }

  protected void Page_Unload(object sender, EventArgs e)

  {

  TestClass.EventList.Add("ASP.NET Page: Unload");

  }

  }

  5. 修改web.config内容如下:

  

复制代码 代码如下:

  <configuration>

  <system.web>

  <compilation debug="true" targetFramework="4.0"/>

  </system.web>

  <system.web>

  <httpModules>

  <add name="TestClass" type="TestClass"/>

  </httpModules>

  </system.web>

  </configuration>

  6. Ctrl+F5执行,在浏览器里可以看到:

验证一个ASP.NET应用程序和页面的生命周期的实现代码

  

  7. 在文本框内输入内容,可得:

   

验证一个ASP.NET应用程序和页面的生命周期的实现代码

  结论:

  1. Module只初始化了一次,当页面postback的时候,module不会再初始化。

  2. Validate和Event事件在页面第一次初始化的时候不会触发,但是由于页面本身存在validate控件和事件按钮,所以这两个事件在第二次会被触发。

  本文参考了codeproject.com的如下一篇文章http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx