页面加载顺序如下:
Page.Init
Page.Load
Textbox.TextChanged
Button.Click
Page.PreRender
Page.Unload
在Page.Unload之前,页面就呈现出来了.
page.init只是在页面第一次载入的时候执行,当页面postback后从page.load开始执行.
e.g
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication8._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lbl" runat="server"></asp:Label> <br /> <asp:Button ID="btn" runat="server" onclick="btn_Click" /> </div> </form> </body> </html>
C#代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication8 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) lbl.Text += "pageload is on.<br />"; else lbl.Text += "This is not your first.<br />"; } protected void Page_Init(object sender, EventArgs e) { lbl.Text += "pageinit is on.<br />"; } protected void Page_PreRender(object sender, EventArgs e) { lbl.Text += "pageprerender is on.<br />"; } protected void btn_Click(object sender, EventArgs e) { lbl.Text += "button is on.<br />"; } } }
执行结果:(点击了button后)