DotnetDude
DotnetDude

Reputation: 11817

base.OnLoad(e) in a ASP.NET page

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?

Upvotes: 5

Views: 12348

Answers (4)

eidylon
eidylon

Reputation: 7248

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.

Upvotes: 2

Eric Nicholson
Eric Nicholson

Reputation: 4123

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

Upvotes: 0

Rob
Rob

Reputation: 45779

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.

Upvotes: 5

sisve
sisve

Reputation: 19781

  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.

Upvotes: 11

Related Questions