返回

asp.net母版页和内容页PageLoad顺序

2014-11-23 asp.net pageload事件 4021 0

关于ASP页面Page_Load发生在事件之前而导致的问题已经喜闻乐见,以下是内容页和母版页(如果有)的事件发生顺序:

ContentPage.PreInit

Master.Init

ContentPage.Init

ContentPage.InitComplite

ContentPage.PreLoad

ContentPage.Load

Master.Load

ContentPage.LoadComplete

ContentPage.PreRender

Master.PreRender

ContentPage.PreRenderComplete

就算知道了发生顺序对新手来说也不能怎么样,那么下面重点就来了:

我们都会发现页面后台有个protected void Page_Load(object sender, EventArgs e)方法

通常大家会把页面载入时需要做的处理代码写在里面,但是这个方法发生在click事件之前,就导致了很多小问题

那么有没有方法是发生在事件之后呢?答案是肯定的:

protected void Page_LoadComplete(object sender, EventArgs e)

是不是很简单?但是这个方法仍然发生在母版加载之前,如果弹出对话框会导致母版排版混乱,那么用下面这个就好了

protected void Page_PreRenderComplete(object sender, EventArgs e)

于是关于生命周期的问题都迎刃而解,代码理解起来也流畅多了

以下是页面载入过程中会激活的一些方法执行顺序,需要在页面特定阶段写代码的话可以用上:

protected void Page_Init(object sender, EventArgs e)

protected void Page_Load(object sender, EventArgs e)

各种用户自定义的控件click事件

protected void Page_LoadComplete(object sender, EventArgs e)

protected void Page_PreRender(object sender, EventArgs e)

protected void Page_PreRenderComplete(object sender, EventArgs e)

protected void Page_Unload(object sender, EventArgs e)

protected void Page_Error(object sender, EventArgs e)

protected void Page_AbortTransaction(object sender, EventArgs e)

protected void Page_CommitTransaction(object sender, EventArgs e)

protected void Page_DataBinding(object sender, EventArgs e)

protected void Page_Disposed(object sender, EventArgs e)

您可能感兴趣:

阿里云 云服务器 99元1年 2核2G 3M固定带宽 续费与新购同价

领取 通义灵码 免费使用资格 兼容 Visual Studio Code、Visual Studio、JetBrains IDEs 等主流编程工具, 为你提供高效、流畅、舒心的智能编码体验!

DOVE 网络加速器 梯子 免费 试用

顶部