为什么ASP.NET控件上的事件处理程序属性的属性有一个前缀(Load事件处理程序的OnLoad)

时间:2022-12-14 15:52:23

This is just for a better understanding of the ASP.NET framework. When you use a control in a declarative way (that would be web form markup), you assign event handlers by their method name using an attribute that starts with On:

这只是为了更好地理解ASP.NET框架。当您以声明方式(即Web表单标记)使用控件时,您可以使用以On开头的属性通过其方法名称分配事件处理程序:

<asp:Button runat="server" OnClick="..."/>

But when you look at the System.Web.UI.WebControls.Button class it has an EventHandler property named Click that the delegate is assigned to:

但是当你查看System.Web.UI.WebControls.Button类时,它有一个名为Click的EventHandler属性,委托被分配给:

button.Click += new EventHandler(...);

So how is this implemented? Is that just a convention followed by the parser?

那么这是如何实现的呢?这只是解析器遵循的约定吗?

I know, it's a strange question, the answer will do nothing but satisfy my curiosity.

我知道,这是一个奇怪的问题,答案只会满足我的好奇心。

1 个解决方案

#1


This is a naming convention used by ASP.NET which, rather unhelpfully, looks identical to another common naming convention widely used throughout .NET. Despite the apparent similarity, these two conventions are unrelated.

这是ASP.NET使用的命名约定,它非常无用,看起来与.NET中广泛使用的另一种常见命名约定相同。尽管有明显的相似性,但这两个公约是无关的。

The .NET-wide convention, which turns out to be irrelevant here, is that it's common for events to have corresponding methods that raise the event, and for those methods' names to be formed by adding an On prefix to the event name. For example, the Click event offered by Button is related to an OnClick method, which raises that event (as has already been stated in another answer here).

.NET范围的约定在这里变得无关紧要,因为事件通常具有引发事件的相应方法,并且通过向事件名称添加On前缀来形成这些方法的名称。例如,Button提供的Click事件与OnClick方法有关,该方法引发该事件(如此处的另一个答案中所述)。

The confusing part is that the OnClick method has nothing to do with the OnClick attribute that the question concerns.

令人困惑的部分是OnClick方法与问题所关注的OnClick属性无关。

It's easy to demonstrate that the OnSomeEvent methods are irrelevant here by writing a control that doesn't have any such method. Here's the codebehind for a simple user control:

通过编写没有任何此类方法的控件,可以很容易地证明OnSomeEvent方法与此无关。这是一个简单的用户控件的代码隐藏:

public partial class EventWithoutMethod : System.Web.UI.UserControl
{
    public event EventHandler Foobar;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Foobar != null)
        {
            Foobar(this, EventArgs.Empty);
        }
    }
}

This declares a Foobar event. (It never actually raises it, but that doesn't matter for the purposes of exploration.) It does not define an OnFoobar method. Nevertheless, ASP.NET is perfectly happy for us to use the OnSomeEvent convention when we use the control:

这宣布了Foobar事件。 (它实际上从未提出它,但这对于探索而言并不重要。)它没有定义OnFoobar方法。然而,当我们使用控件时,ASP.NET非常高兴我们使用OnSomeEvent约定:

<user:EventWithoutMethod runat="server" OnFoobar="FooHandler" />

In fact, it's not only happy for us to do that, it actually requires it. Even though my control doesn't define any member called OnFoobar—the event is called just Foobar—I have to write OnFoobar if I want to attach the event handler from my .aspx file. If I just put a Foobar attribute in there in an attempt to attach the event, the handler will never run. (Unhelpfully, ASP.NET doesn't generate an error when you do that, it just silently fails to do anything with the attribute, and the event handler never runs.)

事实上,它不仅对我们这样做感到高兴,它实际上需要它。即使我的控件没有定义任何名为OnFoobar的成员 - 该事件只被称为Foobar - 如果我想从我的.aspx文件中附加事件处理程序,我必须编写OnFoobar。如果我只是在其中放置Foobar属性以尝试附加事件,则处理程序将永远不会运行。 (无用的是,ASP.NET在您执行此操作时不会生成错误,它只是默默地无法对该属性执行任何操作,并且事件处理程序永远不会运行。)

#1


This is a naming convention used by ASP.NET which, rather unhelpfully, looks identical to another common naming convention widely used throughout .NET. Despite the apparent similarity, these two conventions are unrelated.

这是ASP.NET使用的命名约定,它非常无用,看起来与.NET中广泛使用的另一种常见命名约定相同。尽管有明显的相似性,但这两个公约是无关的。

The .NET-wide convention, which turns out to be irrelevant here, is that it's common for events to have corresponding methods that raise the event, and for those methods' names to be formed by adding an On prefix to the event name. For example, the Click event offered by Button is related to an OnClick method, which raises that event (as has already been stated in another answer here).

.NET范围的约定在这里变得无关紧要,因为事件通常具有引发事件的相应方法,并且通过向事件名称添加On前缀来形成这些方法的名称。例如,Button提供的Click事件与OnClick方法有关,该方法引发该事件(如此处的另一个答案中所述)。

The confusing part is that the OnClick method has nothing to do with the OnClick attribute that the question concerns.

令人困惑的部分是OnClick方法与问题所关注的OnClick属性无关。

It's easy to demonstrate that the OnSomeEvent methods are irrelevant here by writing a control that doesn't have any such method. Here's the codebehind for a simple user control:

通过编写没有任何此类方法的控件,可以很容易地证明OnSomeEvent方法与此无关。这是一个简单的用户控件的代码隐藏:

public partial class EventWithoutMethod : System.Web.UI.UserControl
{
    public event EventHandler Foobar;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Foobar != null)
        {
            Foobar(this, EventArgs.Empty);
        }
    }
}

This declares a Foobar event. (It never actually raises it, but that doesn't matter for the purposes of exploration.) It does not define an OnFoobar method. Nevertheless, ASP.NET is perfectly happy for us to use the OnSomeEvent convention when we use the control:

这宣布了Foobar事件。 (它实际上从未提出它,但这对于探索而言并不重要。)它没有定义OnFoobar方法。然而,当我们使用控件时,ASP.NET非常高兴我们使用OnSomeEvent约定:

<user:EventWithoutMethod runat="server" OnFoobar="FooHandler" />

In fact, it's not only happy for us to do that, it actually requires it. Even though my control doesn't define any member called OnFoobar—the event is called just Foobar—I have to write OnFoobar if I want to attach the event handler from my .aspx file. If I just put a Foobar attribute in there in an attempt to attach the event, the handler will never run. (Unhelpfully, ASP.NET doesn't generate an error when you do that, it just silently fails to do anything with the attribute, and the event handler never runs.)

事实上,它不仅对我们这样做感到高兴,它实际上需要它。即使我的控件没有定义任何名为OnFoobar的成员 - 该事件只被称为Foobar - 如果我想从我的.aspx文件中附加事件处理程序,我必须编写OnFoobar。如果我只是在其中放置Foobar属性以尝试附加事件,则处理程序将永远不会运行。 (无用的是,ASP.NET在您执行此操作时不会生成错误,它只是默默地无法对该属性执行任何操作,并且事件处理程序永远不会运行。)