我们都知道人是有生命的,一个人从呱呱落地到慢慢的老去都要经历一个生命的轮回,这是生命的一个周期。同样,ASP.NET开发的web页面也有它自己的生命周期,从生成道销毁也经历了不同的阶段和过程。
1.独立页面的生命周期事件顺序
Code
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("Page_PreInit<br/>");
}
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Page_Init<br/>");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("Page_InitComplete<br/>");
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("Page_PreLoad<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load<br/>");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("Page_LoadComplete<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page_PreRender<br/>");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("Page_PreRenderComplete<br/>");
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("Page_SaveStateComplete<br/>");
}
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("Page_Unload<br/>");
int i = 0;
i++;//这行代码是用来设置断点的,这里不能用Response.Write
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button事件触发!<br/>");
} 输出结果:
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Page_SaveStateComplete
button事件激发执行顺序
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
Button 事件触发!
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Page_SaveStateComplete
2.具有master页的生命周期事件顺序
Code
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("MasterPage_PreInit<br/>");
}
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("MasterPage_Init<br/>");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("MasterPage_InitComplete<br/>");
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("MasterPage_PreLoad<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("MasterPage_Load<br/>");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("MasterPage_LoadComplete<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("MasterPage_PreRender<br/>");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("MasterPage_PreRenderComplete<br/>");
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("MasterPage_SaveStateComplete<br/>");
}
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("MasterPage_Unload<br/>");
int i = 0;
i++;//这行代码是用来设置断点的,这里不能用Response.Write
} 输出结果:
Page_PreInit
MasterPage_Init
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
MasterPage_Load
Page_LoadComplete
Page_PreRender
MasterPage_PreRender
Page_PreRenderComplete
Page_SaveStateComplete
单击button 事件执行顺序
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_Load
Button 事件触发!
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Page_SaveStateComplete
参考:亮剑.net