Where can I see how events are wired up in ASP.NET

186 Views Asked by At

My noodle had a spark of sentience. I was going through my ASP.NET pages, where I noticed

Hang on, Page_Load is not equal to the name of the class, thus it cannot be the constructor of my class on my aspx.cs page

I have a gut feeling that AutoEventWireup="true" is responsible for telling the page to, by default, call the protected void Page_Load(object sender, EventArgs e) method. The problem (and question) is that I do not know how or where I can view which events are wired up to which handlers. I am sure that AutoEventWireup="true" has this snippet somewhere:

this.Load += this.Page_Load

I am merely seeking to expand my knowledge on this. Where can I see which events are being "wired up" by AutoEventWireup?

Edit

I found the idea after trying to do a virtual constructor call (I made a constructor in my code-behind since I accidentally deleted Page_Load. Resharper suggested I had to seal the class. I thought that it was uncommon behavior. Double checked another page, and copy pasted my Page_Load back in. That's how I wonder how the events are actually wired up. How does ASP.NET know it must call Page_Load?

1

There are 1 best solutions below

1
On BEST ANSWER

This is for .NET 4 other frameworks would be slightly different but poking around with Reflector you can find that TemplateControl which Page and UserControl both inherit has a private method GetDelegateInformationWithNoAssert which wires up these delegates.

private void GetDelegateInformationWithNoAssert(IDictionary dictionary)
{
    if (this is Page)
    {
        this.GetDelegateInformationFromMethod("Page_PreInit", dictionary);
        this.GetDelegateInformationFromMethod("Page_PreLoad", dictionary);
        this.GetDelegateInformationFromMethod("Page_LoadComplete", dictionary);
        this.GetDelegateInformationFromMethod("Page_PreRenderComplete", dictionary);
        this.GetDelegateInformationFromMethod("Page_InitComplete", dictionary);
        this.GetDelegateInformationFromMethod("Page_SaveStateComplete", dictionary);
    }
    this.GetDelegateInformationFromMethod("Page_Init", dictionary);
    this.GetDelegateInformationFromMethod("Page_Load", dictionary);
    this.GetDelegateInformationFromMethod("Page_DataBind", dictionary);
    this.GetDelegateInformationFromMethod("Page_PreRender", dictionary);
    this.GetDelegateInformationFromMethod("Page_Unload", dictionary);
    this.GetDelegateInformationFromMethod("Page_Error", dictionary);
    if (!this.GetDelegateInformationFromMethod("Page_AbortTransaction", dictionary))
    {
        this.GetDelegateInformationFromMethod("OnTransactionAbort", dictionary);
    }
    if (!this.GetDelegateInformationFromMethod("Page_CommitTransaction", dictionary))
    {
        this.GetDelegateInformationFromMethod("OnTransactionCommit", dictionary);
    }
}

If you follow the usages of this method up you will see it gets called from HookUpAutomaticHandlers and this method only attaches the delegates when SupportAutoEvents is true.