• Page.Load和Page_Load差异


     public partial class WebForm1 : System.Web.UI.Page
     {
    
            public WebForm1()
            {
                Load+=new EventHandler(Page_LOAD);
                Load += new EventHandler(Page_Load);
            }
            protected void Page_lOAD(object sender, EventArgs e)
            {
                Response.Write("1");
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("3");
            }
            protected void Page_LOAD(object sender, EventArgs e)
            {
                Response.Write("2");
            } 
      }

     1.  执行WebForm1页面的构造函数WebForm1()

          将Page_LOAD方法和Page_Load方法依次注册到Page.Load委托事件中,可以允许同一方法被多次注册到Load中,执行Load时会仍然全部按照注册的方法去执行。

    2.  查找页面中的Page_Load()方法,添加到字典中(思路,不要我们去查找和添加字典,微软写的方法会做,知道是怎么回事)

         

         我们WebForm1页面中,除了Page_Load()方法,还有Page_lOAD()和Page_LOAD()方法,他们之间是不是有什么联系呢???

         结果发现:查找页面的Page_Load()方法,是以不区分 "Page_Load" 字母大小写的方式去查找,所以,我们按照这种查找方法来,当前页面有3个Page_Load()方法;

         但是只取写在后面的Page_Load()方法,也就是说,我们这里只能取得Page_LOAD()方法。

       也就是说,Page_Load不分大小写,可以写成Page_loAd,同时存在带参数的和无参的,只会取带参数的。

        没有带参数的时候才会去取无参的。如果同时存在名称分别为Page_Load与Page_loAd两个带参(或者都是无参)方法,那么取写在后面的方法(就是在代码中谁写在后面就取谁)。

         如果找到带参数的则添加到字典中,然后返回。如果找不到带参数的,则查找无参的指定名称的方法,找到了添加到字典中。

        带参数的方法签名必须为:Page_Load(object sender, EventArgs e)

        无参的方法签名必须为:Page_Load()

      

        Page_Load的执行时间是在Control类(TemplateControl类的父类)执行完OnLoad方法后执行。

    3.  将字典再与Page.Load事件进行比对,将不重复的方法添加到Page.Load事件。

         根据查找的情况,我们字典中的是Page_LOAD(),将Page_LOAD()方法名和Page.Load事件进行比较;

         结果发现,Page.Load事件存在Page_LOAD()方法,我们就不把Page_LOAD()方法添加到Page.Load事件中。

         只有是和Page.Load事件中不重复(这里的不重复是要区分字母大小写的)的方法才会被添加。

        如果页面上有Page_Load方法,就会Page.Load+=new EventHandler(Page_Load);为Page.Load事件添加了委托方法,Page_Load()方法只会执行一次(因为是以不区分字母大写的方式去寻找的,所以只会有一个在后面的Page_Load()方法会被有字典添加Page_Load事件中)。

       

        假如页面最后的Page_Load()方法是Page_lOAD(),Page.Load+=new EventHandler(Page_lOAD);调用的就是Page_lOAD()方法 

    下面是几个例子:

    例1:

        public partial class WebForm1 : System.Web.UI.Page
        {
    
            public WebForm1()
            {
                Load+=new EventHandler(Page_LOAD);
                Load += new EventHandler(Page_Load);
                Load += new EventHandler(Page_LOAD);
            }
            protected void Page_lOAD(object sender, EventArgs e)
            {
                Response.Write("1");
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("3");
            }
            protected void Page_LOAD(object sender, EventArgs e)
            {
                Response.Write("2");
            } 
        }

    执行结果:232
    首先执行构造函数,依次添加Page_LOAD()、Page_Load()、Page_LOAD()方法到Page.Load事件的委托中;

    查找Page_Load()方法,把最后面的Page_LOAD()方法放到字典中,和Page.Load事件比较,结果事件存在Page_LOAD()了,就不添加到Page.Load事件的委托中,因为重复的方法是不会添加到Load事件的委托链中。然后依次执行Page.Load事件的方法,执行结果就是232

    验证了构造函数添加事件可以多次添加同一方法,查找Page_Load()方法,以不分字母大小写的方式查找,只把页面最后的Page_Load()方法,即Page_LOAD()方法放到字典中,

    Page.Load事件中存在的就不会再次被添加。

     例2:

     public partial class WebForm1 : System.Web.UI.Page
        {
    
            public WebForm1()
            {
                Load+=new EventHandler(Page_LOAD);
                Load += new EventHandler(Page_Load);
                Load += new EventHandler(Page_LOAD);
            }
            //protected void Page_lOAD(object sender, EventArgs e)
            //{
            //    Response.Write("1");
            //}
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("3");
            }
            protected void Page_LOAD(object sender, EventArgs e)
            {
                Response.Write("2");
            }
            protected void Page_lOAD(object sender, EventArgs e)
            {
                Response.Write("1");
            }
        }

    执行结果:2321

    首先还是执行构造函数,添加3个方法到委托中,查找Page_Load()方法,将Page_lOAD()方法添加到字典中,Page.Load事件中没有和Page_lOAD()方法名相同的方法,所以可以添加。然后依次执行Page.Load事件的方法,执行结果就是2321

     例3:

        public partial class WebForm1 : System.Web.UI.Page
        {
    
            public WebForm1()
            {
                //Load+=new EventHandler(Page_LOAD);
                //Load += new EventHandler(Page_Load);
                //Load += new EventHandler(Page_LOAD);
            }
            //protected void Page_lOAD(object sender, EventArgs e)
            //{
            //    Response.Write("1");
            //}
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("3");
            }
            protected void Page_LOAD(object sender, EventArgs e)
            {
                Response.Write("2");
            }
            protected void Page_lOAD(object sender, EventArgs e)
            {
                Response.Write("1");
            }
        }

    执行结果:1

    首先执行构造函数,构造函数为空,就直接查找Page_Load()方法,按照查找的方式,将Page_lOAD()方法添加到字典中,和Page.Load事件中的方法进行比较,没有重复的,添加到Page.Load事件的委托中。执行Page.Load事件的方法,结果为1

    Page_Load方法不分大小写,多个带参的Page_Load方法只取最后一个

    例4:

     public partial class WebForm1 : System.Web.UI.Page
        {
    
            public WebForm1()
            {
                //Load+=new EventHandler(Page_LOAD);
                //Load += new EventHandler(Page_Load);
                //Load += new EventHandler(Page_LOAD);
            }
            //protected void Page_lOAD(object sender, EventArgs e)
            //{
            //    Response.Write("1");
            //}
    
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("3");
            }
            protected void Page_LOAD()
            {
                Response.Write("2");
            }
            //protected void Page_LOAD(object sender, EventArgs e)
            //{
            //    Response.Write("2");
            //}
            //protected void Page_lOAD(object sender, EventArgs e)
            //{
            //    Response.Write("1");
            //}
        }

    执行结果:3

    因为如果存在带参的Page_Load,就不去管无参的了

    http://www.cnblogs.com/jintianhu/archive/2010/12/10/1902078.html中讲述了其中的具体细节,向大牛们继续学习~~~

  • 相关阅读:
    Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
    Codeforces Round #360 (Div. 2) E. The Values You Can Make dp
    Codeforces Round #360 (Div. 2) D. Remainders Game 数学
    Codeforces Round #360 (Div. 2) C. NP-Hard Problem 水题
    Codeforces Round #360 (Div. 2) B. Lovely Palindromes 水题
    Codeforces Round #360 (Div. 2) A. Opponents 水题
    Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
    Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力
    Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
    Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
  • 原文地址:https://www.cnblogs.com/caixiaofeng/p/3321450.html
Copyright © 2020-2023  润新知