详细展示Asp.net页面的生命周期[转载]
2009-04-17 10:03 周国选 阅读(282) 评论(0) 编辑 收藏 举报对于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 e)
48 {
49 Response.Write("Page_SaveStateComplete<br/>");
50
51 }
52
53
54 protected void Page_Unload(object sender, EventArgs e)
55 {
56 int i = 0;
57 i++;//这行代码是用来设置断点的,为什么不用Response.Write?你说呢?
58
59 }
60
61
62 protected void Button1_Click(object sender, EventArgs e)
63 {
64 Label1.Text = "ControlEvent";
65 Response.Write("Button事件触发!<br/>");
66 }
67}
68
69
70
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 e)
48 {
49 Response.Write("Page_SaveStateComplete<br/>");
50
51 }
52
53
54 protected void Page_Unload(object sender, EventArgs e)
55 {
56 int i = 0;
57 i++;//这行代码是用来设置断点的,为什么不用Response.Write?你说呢?
58
59 }
60
61
62 protected void Button1_Click(object sender, EventArgs e)
63 {
64 Label1.Text = "ControlEvent";
65 Response.Write("Button事件触发!<br/>");
66 }
67}
68
69
70
运行结果:
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
Page_LoadComplete
Page_PreRender
Page_SaveStateComplete
点击页面的Button后的输出:
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
Button事件触发!
Page_LoadComplete
Page_PreRender
Page_SaveStateComplete