如何在ASP.NET控件来为事件处理性能的属性有preFIX(的OnLoad的Load事件处理程序) [英] How come the attributes for event handler properties on ASP.NET controls have a prefix (OnLoad for the Load event handler)
问题描述
这仅仅是为了更好地理解ASP.NET框架中。当你在一个声明的方式使用控制(这将是网页形式的标记),您使用与始于
属性按自己的方法名称分配事件处理程序:
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:Button runat="server" OnClick="..."/>
但是,当你看System.Web.UI.WebControls.Button类有一个名为事件处理程序属性点击
的委托分配给:
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.
推荐答案
这是由ASP.NET使用的命名约定当中,而帮倒忙,看起来与另一种常见的命名约定.NET各地广泛使用。尽管有明显的相似性,这两个公约是不相关的。
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.
在.NET范围内的惯例,这原来是这里无关紧要,就是它很常见的事件,有引发事件对应的方法,以及通过增加一个在
preFIX事件名称。例如,点击由
是关系到一个按钮提供
事件的OnClick
方法,这引起了该事件(如已经在这里另一个答案说明)。
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).
混乱的部分是,的OnClick
的方法的有什么做的的OnClick
的属性的,这个问题的关注。
The confusing part is that the OnClick
method has nothing to do with the OnClick
attribute that the question concerns.
这很容易证明 OnSomeEvent
方法是这里无关紧要通过编写没有任何这样的方法控制。下面是一个简单的用户控件codebehind:
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:
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);
}
}
}
这声明了一个 Foobar的
事件。 (它从来没有真正提高,但不要紧勘探的目的。)它做的不可以定义一个 OnFoobar
方法。尽管如此,ASP.NET是完全高兴,当我们使用控制我们使用 OnSomeEvent
约定:
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:
<user:EventWithoutMethod runat="server" OnFoobar="FooHandler" />
事实上,这不仅高兴我们做到这一点,它实际上要求的它。虽然我的控制并没有定义任何叫做成员 OnFoobar
-the事件被称为刚 Foobar的
- 我不得不写 OnFoobar
如果我想从我的的.aspx
文件附加事件处理程序。如果我只是把一个 Foobar的
属性在里面,试图连接的情况下,处理程序将永远不会运行。 (帮倒忙,ASP.NET不会生成一个错误,当你做到这一点,它只是默默地失败的属性做任何事,和事件处理程序从未运行。)
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.)
这篇关于如何在ASP.NET控件来为事件处理性能的属性有preFIX(的OnLoad的Load事件处理程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!