ASP.NET 页面执行顺序详解

今天整理了一下ASP执行过程,从.net页码的执行周期开始做一个详细的了解.我重写了页面的绝大多数方法.然后加载执行.所得的顺序如下。

方法是每个重写的事件中都输出一个字符,按字符打印出来的先后判断事件执行的顺序。如有不正确之处,还请路过的各位不吝赐教)

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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:button runat="server" text="Button" onclick="Unnamed1_Click" />
    </div>
    </form>
</body>
</html>
View Code

后台代码

    public partial class WebForm1 : System.Web.UI.Page
    {
        private static int count = 0;
        //6
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(count + ".Page_Load <br />");
            count++;
        }
        
        //2
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            Response.Write(count + ".OnPreInit <br />");
            count++;
        }
        //3
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Response.Write(count + ".OnInit <br />");
            count++;
        }
        //7
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Response.Write(count + "OnLoad<br/>");
            count++;
        }
        //5
        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            Response.Write(count + "OnPreLoad<br/>");
            count++;
        }
        //4   9
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            Response.Write(count + "OnLoadComplete<br/>");
            count++;
        }

        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
            Response.Write(count + "OnInitComplete<br/>");
            count++;
        }
        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            //Response.Write(count + "OnUnload<br/>");
            count=99999999;
        }
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            Response.Write(count + "OnDataBinding<br/>");
            count++;
        }
        //10
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Response.Write(count + "OnPreRender<br/>");
            count=1111;
        }
        //8
        protected void Unnamed1_Click(object sender, EventArgs e)
        {

            Response.Write(count + "Unnamed1_Click<br/>");
            count++;
        }
   
    }
View Code

根据源码可以看到asp页面的执行顺序,

其实是个很简单的内容。顺便写下Page事件:

事件处理器名称

发生时间

Page_Init

在Web窗体的视图状态加载服务器控件并对其初始化。

这是web窗体生命周期的第一步

Page_Load

在Page对象上载入服务器控件。由于此时视图状态信息是可以使用的,

因此载这里可以用代码来改变空间的设置或者载页面上显示文本。

Page_PreRender

应用程序将要呈现Page对象

Page_Unload

页面从内存中卸载

Page_Error

发生未处理的异常

Page_AbortTransaction

事务处理被终止

Page_CommitTransaction

事务处理被接受

Page_DataBinding

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

Page_Disposed

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

 

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

 

 

参考     关于asp.net中页面事件加载的先后顺序

posted @ 2013-12-26 17:08  母鸡轰  阅读(2237)  评论(1编辑  收藏  举报