ASP.NET4.5从零开始(2)
认识生命周期
引言
之前关于生命周期的了解都是基于项目的需求,临时去百度上了解的,所以一直想要更加系统地去学些一下。本文,将通过一个简单的例子来对生命周期做一个直观的展现。
开始前的准备
创建Web Form项目
使用ASP.NET EMPTY WEB APPLICATION模板新建一个Visual Studio项目:

在项目上右键-Add-New Item选择Web模板中的Web Form项目
正文
下表列举了一个Page对象大体经过的阶段(不包含Application的生命周期)
|
Stage |
Description |
|---|---|
|
Page request |
The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page. |
|
Start |
In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property. |
|
Initialization |
During page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state. |
|
Load |
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state. |
|
Postback event handling |
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.) |
|
Rendering |
Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Rendermethod for each control, providing a text writer that writes its output to the OutputStream object of the page's Responseproperty. |
|
Unload |
The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed. |
ok,下面通过项目来分别看看各个阶段
1.项目上右键-添加-Web窗体
代码如下:
1 public partial class WebForm1 : System.Web.UI.Page 2 { 3 private List<string> EventList = new List<string>(); 4 /// <summary> 5 /// Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time. 6 /// Create or re-create dynamic controls. 7 /// Set a master page dynamically. 8 /// Set the Theme property dynamically. 9 /// Read or set profile property values. 10 /// </summary> 11 /// <param name="sender"></param> 12 /// <param name="e"></param> 13 protected void Page_PreInit(object sender, EventArgs e) 14 { 15 EventList.Add("Page_PreInit"); 16 } 17 18 /// <summary> 19 /// Use this event to read or initialize control properties. 20 /// </summary> 21 /// <param name="sender"></param> 22 /// <param name="e"></param> 23 protected void Page_Init(object sender, EventArgs e) 24 { 25 EventList.Add("Page_Init"); 26 } 27 28 /// <summary> 29 /// Use this event to make changes to view state that you want to make sure are persisted after the next postback. 30 /// </summary> 31 /// <param name="sender"></param> 32 /// <param name="e"></param> 33 protected void Page_InitComplete(object sender, EventArgs e) 34 { 35 EventList.Add("Page_InitComplete"); 36 } 37 38 protected void Page_PreLoad(object sender, EventArgs e) 39 { 40 EventList.Add("Page_PreLoad"); 41 } 42 43 /// <summary> 44 /// Use the OnLoad event method to set properties in controls and to establish database connections. 45 /// </summary> 46 /// <param name="sender"></param> 47 /// <param name="e"></param> 48 protected void Page_Load(object sender, EventArgs e) 49 { 50 EventList.Add("Page_Load"); 51 52 } 53 54 /// <summary> 55 /// Use this event for tasks that require that all other controls on the page be loaded. 56 /// </summary> 57 /// <param name="sender"></param> 58 /// <param name="e"></param> 59 protected void Page_LoadComplete(object sender, EventArgs e) 60 { 61 EventList.Add("Page_LoadComplete"); 62 } 63 64 /// <summary> 65 /// Use the event to make final changes to the contents of the page or its controls before the rendering stage begins. 66 /// </summary> 67 /// <param name="sender"></param> 68 /// <param name="e"></param> 69 protected void Page_PreRender(object sender, EventArgs e) 70 { 71 EventList.Add("Page_PreRender"); 72 } 73 74 protected void Page_PreRenderComplete(object sender, EventArgs e) 75 { 76 EventList.Add("Page_PreRenderComplete"); 77 } 78 79 protected void Page_SaveStateComplete(object sender, EventArgs e) 80 { 81 EventList.Add("Page_SaveStateComplete"); 82 } 83 84 protected override void Render(HtmlTextWriter writer) 85 { 86 EventList.Add("Render"); 87 eventRepeater.DataSource = EventList; 88 eventRepeater.DataBind(); 89 base.Render(writer); 90 } 91 92 /// <summary> 93 /// use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks. 94 /// </summary> 95 /// <param name="sender"></param> 96 /// <param name="e"></param> 97 protected void Page_Unload(object sender, EventArgs e) 98 { 99 EventList.Add("Page_Unload"); 100 } 101 }
代码很简单用了一个List对象记录了各个生命周期事件,不包括(Error等特殊事件),前台通过一个repeater控件对事件过程做了展示,效果如下图(注意:如果将数据绑定放在Upload事件中,将没有效果):

最后用一张图,对Page以及其中的Control对象生命周期做一个总结

总结语
本章简要的对Page的生命周期做了一个介绍,不过有些方面说的不够深入,下一篇将通过一些例子对其进行深入,并探索一下控件及其数据绑定在生命周期中是如何运作的。

浙公网安备 33010602011771号