Asp.NET页面事件加载的顺序

  介绍

这篇文章主要介绍了Asp.NET页面事件加载的顺序,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。

<强> ASP。净母版页和内容页中的事件

我们知道母版页和内容页都可以包含控件的事件处理程序。对于控件而言内容页中的控件在内容页中引发事件,母版页中的控件在母版页中引发事件。控件事件不会从内容页发送到母版页,也不能在内容页中处理来自母版页控件的事件,它们只会在自己事件内部进行处理。

<强>下面是母版页(主人)与内容页(ContentPage)合并后事件的发生顺序:

主页面控件,Init 事件。   ContentPage页面控件,Init 事件。   主人页,,Init 事件。   ContentPage页,Init 事件。   ContentPage页,Load 事件。   主人页,,Load 事件。   ContentPage页面控件,Load 事件。   ContentPage页面,PreRender 事件。   主人页面,,PreRender 事件。   主人页面控件,PreRender 事件。   ContentPage页面控件,PreRender 事件。

母版页和内容页中的事件顺序对于页面开发人员并不重要。但是,如果创建的事件处理程序取决于某些事件的可用性,了解母版页和内容页中的事件顺序很有帮助。

<强> Asp。净中页面事件加载的先后顺序

1,单独一个页面执行中将按照如下顺序激活事件:

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

2,如果页面从另一个页面继承,如<代码> BasePage: System.Web.UI。页> BasePage.PreInit   Page.PreInit   BasePage.Init   Page.Init   BasePage.InitComplite   Page.InitComplite   BasePage.PreLoad   Page.PreLoad   BasePage.Load   Page.Load   BasePage.LoadComplete   Page.LoadComplete   BasePage.PreRender   Page.PreRender   BasePage.PreRenderComplete   页面。PreRenderComplete

3,如果使用了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

<强>需要注意的是主里面是没有PreInit事件。

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

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

只需要记住:先加载继承页,再加载自己,如果继承页有继承则先加载继承页的继承。

事件处理器名称发生时间Page_Init在Web窗体的视图状态加载服务器控件并对其初始化。这是窗体生命周期的第一步Page_Load在页面上对象上载入服务器控件。由于此时视图状态信息是可以使用的,因此载这里可以用代码来改变空间的设置或者载页面上显示文本.Page_PreRender应用程序将要呈现页面

Page_Unload

页面从内存中卸载

Page_Error

发生未处理的异常Page_AbortTransaction

事务处理被终止Page_CommitTransaction

事务处理被接受

Page_DataBinding

把页面上的服务器空间和数据源绑定载一起

Page_Disposed

页对象从内存中释放掉。这是页面对象生命周期中的最后一个事件

<强> Init、负载、PreRender事件执行顺序:

1)控件的Init事件

2)控件所在页面的Init事件

3)控件所在页面的负载事件

4)控件的载荷事件

5)控件所在页面的PreRender事件

6)控件的PreRender事件

<强>个人研究的一些心得体会:(下面的两点可以通过自建页面并重写相关事件进行验证)

Asp.NET页面事件加载的顺序