views:

888

answers:

4

I might have misunderstood the meaning of base.OnLoad(e); My understanding was that this statement will call the OnLoad method of the base class of the class where it's called from. However, when I use the debugger to step through the code, I see different results.

public abstract class BaseUC : System.Web.UI.UserControl
{
   protected override void OnLoad(EventArgs e)
   {
    base.OnLoad(e);

    SomeAbstractMethod();
   }
}

In the ascx.cs concrete class

public partial class MyUserControl : BaseUC
{
    protected void Page_Load(object sender, EventArgs e)
    {
     //On Load logic
    }
}

I have a breakpoint on base.OnLoad(e). When I press F11 (step into), the debugger takes me to Page_Load of MyUserControl, so the flow of control is:

BaseUC.OnLoad()
MyUserControl.Page_Load()
BaseUC.SomeAbstractMethod()

Can someone explain what's going on here?

+8  A: 
  1. BaseUC.Onload calls Control.OnLoad which triggers the Load event.
  2. The Page_Load method works due to AutoEventWireUp=True and executes when the Load event executes.
  3. BaseUC will then continue execution, calling SomeAbstractMethod.
Simon Svensson
"BaseUC.Onload calls Control.OnLoad which triggers the Load event." which line is making this happen?
DotnetDude
base.OnLoad(e) - the code in there will raise the event.
Joel Coehoorn
I guess that's the part I don't understand. Why does base.OnLoad(e) call the Control.OnLoad (in this case the MyUserControl.Page_Load())?
DotnetDude
@DotnetDude, every overriden implementation of OnLoad above you contains a call to base.OnLoad, until it reaches Control.OnLoad which triggers the Load event.
Simon Svensson
Is it safe to say that the base.OnLoad(e) raises an event and all the subscribers (MyUserControl.Page_Load()) to this event are notified and that's why the Page_Load is executed?
DotnetDude
DotnetDude, that's both safe to say, and entirely correct. The topmost OnLoad will fire the Load event (it's a somewhat common theme to name methods that raise the event Xxx OnXxx).
Simon Svensson
And Page_Load() subscribing to the OnLoad() event is handled by the AutoEventWireUp? In theory can I have my own method subscribe to the OnLoad() event as well?
DotnetDude
@DotnetDude, there are requirements for the naming of the automatically wired up methods (Page_Init, Page_Load). But you can attach any method (that fits the signature of the event) to Load, or even several methods if you want. Take a look at http://stackoverflow.com/questions/680878/what-does-autoeventwireup-page-property-mean where AutoEventWireUp have been discussed before.
Simon Svensson
+3  A: 

Page_Load and OnLoad are different things =)

Page_Load is a method that exists on the page which is called by the ASP.net runtime (thanks to the magic of AutoEventWireUp) OnLoad is the method that raises the Load event, so by putting code before the base.OnLoad(e) call you can cause code to execute prior to the event being raised, after it for code to run after the event is raised.

Take a look at this blog entry from Infinities Loop entry on weblogs.asp.net about this for a bit of a broader explanation.

Rob
A: 

Do you have the debug symbols loaded for System.Web? F11 won't step into code you don't have loaded.

Eric Nicholson
-1 definately not whats going on here... Other answers got the right idea
LorenVS
It's the most obvious part of the problem. The OP is trying to step into a method in the BCL...The other posters are absolutely correct about the call to Page_Load, but it's important to understand what the debugger is doing too.
Eric Nicholson
Being able to step into System.Web would have shown the firing of the Load event and that Page_Load will react on it.
Simon Svensson
+2  A: 

Curiously, what are you expecting to see? I don't think you'll see it step in to base.OnLoad(e), since your base class at that point is System.Web.UI.UserControl, and that is a system class, ... so you most likely don't have the source code for that to be able to step into it.

eidylon
I expected:BaseUC.OnLoad()BaseUC.SomeAbstractMethod()MyUserControl.Page_Load()
DotnetDude