Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Strange Page_Load behavior

Strange Page_Load behavior

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    parsiphal
    wrote on last edited by
    #1

    Hi all, here I am with a new trouble I do not understand, seeking for your kindly help. I have 3 userControl, say A, B, C. C inherits from B, which in turn intherits form A. I have a WebForm that instances userControl C. Case One: with 3 Page_Load methods private and separated In this case I can see execution sequence is C, A, B. Why? It should be rather be A, B, C or C, B, A in my opinion. Case two: overriding Page_Load In this case, making each override to call first its base, I can see the sequence A, B, C called trice. Case tre: using protected new Page_Load In this case is particular visible that, regardless what you can try, the underlaying sequence is always C, A, B: you can obtain some variation like (B,C),A,B - where B,C is in place of C in the original sequence, for example because you have specified a line like base.Page_Load in class C. It is a really strange behavior. Always things are strange when you do not understand them! ;) But it makes me clear the degrease of confusion I still have concerning Page_Load (you see, I have casted to Page_Load the same logic of Init and of constructors). My trouble is fixed passing my code in Init. But I'm curious to understand this event Load... and its behavior so strange to me. I really apreciate any help. Have a nice day, Parsiphal

    S M 2 Replies Last reply
    0
    • P parsiphal

      Hi all, here I am with a new trouble I do not understand, seeking for your kindly help. I have 3 userControl, say A, B, C. C inherits from B, which in turn intherits form A. I have a WebForm that instances userControl C. Case One: with 3 Page_Load methods private and separated In this case I can see execution sequence is C, A, B. Why? It should be rather be A, B, C or C, B, A in my opinion. Case two: overriding Page_Load In this case, making each override to call first its base, I can see the sequence A, B, C called trice. Case tre: using protected new Page_Load In this case is particular visible that, regardless what you can try, the underlaying sequence is always C, A, B: you can obtain some variation like (B,C),A,B - where B,C is in place of C in the original sequence, for example because you have specified a line like base.Page_Load in class C. It is a really strange behavior. Always things are strange when you do not understand them! ;) But it makes me clear the degrease of confusion I still have concerning Page_Load (you see, I have casted to Page_Load the same logic of Init and of constructors). My trouble is fixed passing my code in Init. But I'm curious to understand this event Load... and its behavior so strange to me. I really apreciate any help. Have a nice day, Parsiphal

      S Offline
      S Offline
      S Sansanwal
      wrote on last edited by
      #2

      The problem is till page_load, the page don't know about the controls parent. It does this all work in OnInit function. Sanjay Sansanwal www.sansanwal.com

      1 Reply Last reply
      0
      • P parsiphal

        Hi all, here I am with a new trouble I do not understand, seeking for your kindly help. I have 3 userControl, say A, B, C. C inherits from B, which in turn intherits form A. I have a WebForm that instances userControl C. Case One: with 3 Page_Load methods private and separated In this case I can see execution sequence is C, A, B. Why? It should be rather be A, B, C or C, B, A in my opinion. Case two: overriding Page_Load In this case, making each override to call first its base, I can see the sequence A, B, C called trice. Case tre: using protected new Page_Load In this case is particular visible that, regardless what you can try, the underlaying sequence is always C, A, B: you can obtain some variation like (B,C),A,B - where B,C is in place of C in the original sequence, for example because you have specified a line like base.Page_Load in class C. It is a really strange behavior. Always things are strange when you do not understand them! ;) But it makes me clear the degrease of confusion I still have concerning Page_Load (you see, I have casted to Page_Load the same logic of Init and of constructors). My trouble is fixed passing my code in Init. But I'm curious to understand this event Load... and its behavior so strange to me. I really apreciate any help. Have a nice day, Parsiphal

        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #3

        Hi parsiphal, It seems to me that the Page_Load event handler in the control A is registered to the Page.Load event before the same thing happens in the control B. So the execution sequence of the Page_Load event hanlers at run time is: C, A, B. In fact, this order depends on the way you wire up the Page_Load event handler to the event in the controls, then it can be: C, B, A or A, B, C. And the normal sequence is: C, B, A, the sample code below demonstrate that:

        public class A : System.Web.UI.UserControl
        {
        private void Page_Load(object sender, System.EventArgs e)
        {
        Response.Write("Page_Load : A ");
        }

        override protected void OnInit(EventArgs e)
        {
        	InitializeComponent();
        	base.OnInit(e);
        }
                     
        private void InitializeComponent()
        {
        	this.Load += new System.EventHandler(this.Page\_Load);
        
        }
        

        }

        public class B : A
        {
        //The sample code here is the same as the control A
        }

        public class C : B
        {
        //The sample code here is the same as the control A
        }

        At run time, the control C is dynamically loaded in the Page_Load event handler of a test web form. The order of the Page_Load of the controls is: C, B, A. Now, if you happen to change the order of the code in the OnInit of the control B like this:

        override protected void OnInit(EventArgs e)
        {
        base.OnInit(e);
        InitializeComponent();
        //base.OnInit(e);
        }

        then the order is C, A, B. It is because the base.OnInit(e) is called prior to the InitializeComponent() method, so the Page_Load event handler in the control A is registered before the handler is wired up in the control B. Here I assume that I don't make any call to the OnLoad method of the base class like base.OnLoad (I basically do that only when I override the method of the base class). So you may look at your sample code again and try to watch the order of the Page_Load event handlers when they are registered to the event in the controls, then you will understand that.

        P 1 Reply Last reply
        0
        • M minhpc_bk

          Hi parsiphal, It seems to me that the Page_Load event handler in the control A is registered to the Page.Load event before the same thing happens in the control B. So the execution sequence of the Page_Load event hanlers at run time is: C, A, B. In fact, this order depends on the way you wire up the Page_Load event handler to the event in the controls, then it can be: C, B, A or A, B, C. And the normal sequence is: C, B, A, the sample code below demonstrate that:

          public class A : System.Web.UI.UserControl
          {
          private void Page_Load(object sender, System.EventArgs e)
          {
          Response.Write("Page_Load : A ");
          }

          override protected void OnInit(EventArgs e)
          {
          	InitializeComponent();
          	base.OnInit(e);
          }
                       
          private void InitializeComponent()
          {
          	this.Load += new System.EventHandler(this.Page\_Load);
          
          }
          

          }

          public class B : A
          {
          //The sample code here is the same as the control A
          }

          public class C : B
          {
          //The sample code here is the same as the control A
          }

          At run time, the control C is dynamically loaded in the Page_Load event handler of a test web form. The order of the Page_Load of the controls is: C, B, A. Now, if you happen to change the order of the code in the OnInit of the control B like this:

          override protected void OnInit(EventArgs e)
          {
          base.OnInit(e);
          InitializeComponent();
          //base.OnInit(e);
          }

          then the order is C, A, B. It is because the base.OnInit(e) is called prior to the InitializeComponent() method, so the Page_Load event handler in the control A is registered before the handler is wired up in the control B. Here I assume that I don't make any call to the OnLoad method of the base class like base.OnLoad (I basically do that only when I override the method of the base class). So you may look at your sample code again and try to watch the order of the Page_Load event handlers when they are registered to the event in the controls, then you will understand that.

          P Offline
          P Offline
          parsiphal
          wrote on last edited by
          #4

          Hi there, as always yours is a keen answer, and your kindness is big. You have centered the point, and I was far from the truth 'cause I was not thinking of delegates. I want to thank you strongly, for you have make me jump from a level of thinking to a higher one, changing my perspective respect to this and other related questions with ASP.NET and OOP paradigm's implementation. Thankyou a lot. Parsiphal

          M 1 Reply Last reply
          0
          • P parsiphal

            Hi there, as always yours is a keen answer, and your kindness is big. You have centered the point, and I was far from the truth 'cause I was not thinking of delegates. I want to thank you strongly, for you have make me jump from a level of thinking to a higher one, changing my perspective respect to this and other related questions with ASP.NET and OOP paradigm's implementation. Thankyou a lot. Parsiphal

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Thank you for the kind words :-D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups