您的位置:首页 > 编程语言 > ASP

【转】ASP.NET 2.0中Page事件的执行顺序

2011-05-08 21:53 363 查看
有些补充的是,控件的一些事件是在Page的PreRender之前的,比如说SqlDataSource的DataBind,所以在页面PreRender内,是有机会修改这些控件的参数的。但是可以再PreRender中提前执行控件的数据绑定。

为何会有以下两种说法,不太明白。

似乎页面的PreRender发生在控件的PreRender之前,所以第一种说法可能比较靠谱。

Init,Load,PreRender事件执行顺序:
1)控件的Init事件
2)控件所在页面的Init事件
3)控件所在页面的Load事件
4)控件的Load事件
5)控件所在页面的PreRender事件
6)控件的PreRender事件

规律:
1)Init事件从最里面的控件(包括用户控件及普通控件)向最外面的控件(页面)引发,Load及PreRender等其他事件从最外面的控件向最里面的控件引发;
2)控件之间相同事件的执行顺序依控件在页面的位置按从左到右,从上到下的先后顺序执行。

注意:
1)切记用户控件也被视为页面中的一个控件;
2)把用户控件作为单独的一个特殊页面来看,它本身及其所包含的控件同样遵守相同的规律;
3)有时在客户端程序(如javascript)中会用到客户端body对像的onload事件,注意这个客户端事件是最后执行,即在服务器端所有事件执行完后才执行。

-------------------------------------------------------------------------------------------------------------------

Page 执行中将按照如下顺序激活事件:

Page.PreInit
Page.Init
Page.InitComplite
Page.PreLoad
Page.Load
Page.LoadComplete
Page.PreRender
Page.PreRenderComplete

如果页面从令一个页面继承,如BasePage:System.Web.UI.Page,在BasePage中做了一些扩展,如权限检查,而其他页面从BasePage继承,则BasePage和最终Page的事件激活顺序是:

UI.PreInit
Page.PreInit
UI.Init
Page.Init
UI.InitComplite
Page.InitComplite
UI.PreLoad
Page.PreLoad
UI.Load
Page.Load
UI.LoadComplete
Page.LoadComplete
UI.PreRender
Page.PreRender
UI.PreRenderComplete
Page.PreRenderComplete

如果使用了MasterPage,则MasterPage中的事件和ContentPage中的事件按照下面顺序激活:

ContentPage.PreInit
Master.Init
ContentPage.Init
ContentPage.InitComplite
ContentPage.PreLoad
ContentPage.Load
Master.Load
ContentPage.LoadComplete
ContentPage.PreRender
Master.PreRender
ContentPage.PreRenderComplete

更进一步,如果ContentPage继承BasePage,那么,各事件的执行顺序将变成:

UI.PreInit
ContentPage.PreInit
Master.Init
UI.Init
ContentPage.Init
UI.InitComplite
ContentPage.InitComplite
UI.PreLoad
ContentPage.PreLoad
UI.Load
ContentPage.Load
Master.Load
UI.LoadComplete
ContentPage.LoadComplete
UI.PreRender
ContentPage.PreRender
Master.PreRender
UI.PreRenderComplete
ContentPage.PreRenderComplete
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: