close
寫了一個小程式,讓網頁週期事件的順序一目了然:
# Default.aspx
<body> <form id="form1" runat="server"> <div> <br /> <% Response.Write("<hr/> 這是 Inline Code 寫法 <hr />"); %> <hr /> <asp:Button ID="Button1" runat="server" Text="Response.WriteFile" OnClick="Button1_Click" /> <hr /> <asp:Button ID="Button2" runat="server" Text="Response.Redirect" OnClick="Button2_Click" /> <hr /> <asp:Button ID="Button3" runat="server" Text="Response.End" OnClick="Button3_Click" /> </div> </form> </body>
# Default.aspx.cs
protected void Page_PreInit(object s, EventArgs e) { Response.Write("<br>1 *** 這是 Page_PreInit ***<br>"); } protected void Page_Init(object sender, EventArgs e) { Response.Write("<br>2 *** 這是 Page_Init ***<br>"); } protected void Page_InitComplete(object sender, EventArgs e) { Response.Write("<br>3 *** 這是 Page_InitComplete ***<br>"); } protected void Page_PreLoad(object sender, EventArgs e) { Response.Write("<br>4 *** 這是 Page_PreLoad ***<br>"); } protected void Page_Load(object sender, EventArgs e) { Response.Write("<br>5 *** 這是 Page_Load ***<br>"); } protected void Page_LoadComplete(object s, EventArgs e) { Response.Write("<br>6 *** 這是 Page_LoadComplete ***<br>"); } protected void Page_PreRender(object s, EventArgs e) { Response.Write("<br>7 *** 這是 Page_PreRender ***<br>"); } protected void Page_PreRenderComplete(object s, EventArgs e) { Response.Write("<br>8 *** 這是 Page_PreRenderComplete ***<br>"); } protected void Page_SaveStateComplete(object s, EventArgs e) { Response.Write("<br>9 *** 這是 Page_SaveStateComplete ***<br>"); } protected void Page_Disposed(object s, EventArgs e) { Response.Write("<br>*** 這是 Page_Disposed ***<br>"); } protected void Page_Unload(object s, EventArgs e) { //Response.Write("<br>*** 這是 Page_Unload ***<br>"); } protected void Button1_Click(object sender, EventArgs e) { Response.WriteFile("123.txt"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Default.aspx"); } protected void Button3_Click(object sender, EventArgs e) { Response.End(); Response.Redirect("http://www.find.org.tw"); }
# Demo
圖中可以很清楚看到由上到下的順序,另外我還加了 Inline code寫法 和 WriteFile(),比較執行點。
PS. 詳細的網頁週期說明 Google 一下就很多,這裡就不多說,順便附上官方說明文件:
https://msdn.microsoft.com/zh-tw/library/ms178472(v=vs.100).aspx
以上
* 如有錯誤或不足,歡迎指教~~
文章標籤
全站熱搜
留言列表