在哪里可以看到事件是如何在 ASP.NET 中连接的

本文关键字:ASP NET 连接 事件 在哪里 | 更新日期: 2023-09-27 18:36:36

我的面条有一丝火花。 我正在浏览我的 ASP.NET 页面,我注意到

等等,Page_Load不等于类的名称,因此它不能是我的aspx.cs页面上我的类的构造函数

我有一种直觉,AutoEventWireup="true"负责告诉页面,默认情况下,调用protected void Page_Load(object sender, EventArgs e)方法。 问题(和问题)是我不知道如何或在哪里可以查看哪些事件连接到哪些处理程序。我敢肯定AutoEventWireup="true"某处有这个片段:

this.Load += this.Page_Load

只是想扩大我在这方面的知识。在哪里可以看到 AutoEventWireup 正在"连接"哪些事件?

编辑

我在尝试进行虚拟构造函数调用后找到了这个想法(因为我不小心删除了Page_Load,所以我在我的代码隐藏中创建了一个构造函数。Resharper建议我必须封印这门课。我认为这是不寻常的行为。仔细检查了另一页,然后将我的Page_Load复制粘贴回去。这就是我想知道事件实际上是如何连接的。ASP.NET 怎么知道它一定叫Page_Load

在哪里可以看到事件是如何在 ASP.NET 中连接的

这是

针对 .NET 4 的,其他框架会略有不同,但使用 Reflector,您会发现PageUserControl都继承TemplateControl都有一个私有方法GetDelegateInformationWithNoAssert它连接这些委托。

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);
    }
}

如果您遵循此方法的用法,您将看到它从HookUpAutomaticHandlers调用,并且此方法仅在SupportAutoEvents为 true 时附加委托。