对于Asp.net页面层开发无论是写页面还是写控件,我觉得都可以用一句话描述:"Do the right thing at the right time in the right place."这是07年底的一篇东西,还是有点价值整理出来与大家共享。
本文从两个粒度对Asp.net生命周期做了展示,一是通过记录页面事件的触发顺序看请求的处理流程,一是通过Reflector看Page类内部对请求处理的实现,为了清晰我清理掉了ETW相关的代码保留了一个简化却足可以说明问题的流程骨架;
本文覆盖以下内容:
- 页面事件的触发顺序展示
- 清理掉ETW代码后的,Page类内部对请求处理的实现
- MSDN关于Asp.net生命周期非常重要的四个表格
- 演示源代码下载
1using System;
2using System.Configuration;
3using System.Data;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.HtmlControls;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10
11public partial class _Default : System.Web.UI.Page
12{
13 protected void Page_PreInit(object sender, EventArgs e)
14 {
15 Response.Write("Page_PreInit<br/>");
16 }
17 protected void Page_Init(object sender, EventArgs e)
18 {
19 Response.Write("Page_Init<br/>");
20
21 }
22 protected void Page_InitComplete(object sender, EventArgs e)
23 {
24 Response.Write("Page_InitComplete<br/>");
25
26 }
27 protected void Page_PreLoad(object sender, EventArgs e)
28 {
29 Response.Write("Page_PreLoad<br/>");
30
31 }
32 protected void Page_Load(object sender, EventArgs e)
33 {
34 Response.Write("Page_Load<br/>");
35
36 }
37 protected void Page_LoadComplete(object sender, EventArgs e)
38 {
39 Response.Write("Page_LoadComplete<br/>");
40
41 }
42 protected void Page_PreRender(object sender, EventArgs e)
43 {
44 Response.Write("Page_PreRender<br/>");
45
46 }
47 protected void Page_SaveStateComplete(object sender, EventArgs&n