为什么 ASP.NET 控件上的事件处理程序属性的属性有一个前缀(Load 事件处理程序的 OnLoad)
这只是为了更好地理解 ASP.NET 框架。 当您以声明性方式(即 Web 表单标记)使用控件时,您可以使用以 On
开头的属性按方法名称分配事件处理程序:
<asp:Button runat="server" OnClick="..."/>
但是当您查看 System.Web .UI.WebControls.Button 类有一个名为 Click
的 EventHandler 属性,委托被分配给该属性:
button.Click += new EventHandler(...);
那么这是如何实现的呢? 这只是解析器遵循的约定吗?
我知道,这是一个奇怪的问题,答案除了满足我的好奇心之外没有任何作用。
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="..."/>
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 ASP.NET 使用的命名约定,看起来与 .NET 中广泛使用的另一个常见命名约定相同,但毫无帮助。 尽管表面上很相似,但这两个约定并不相关。
.NET 范围的约定(事实证明在这里无关紧要)是,事件通常具有引发事件的相应方法,并且这些方法的名称通过添加
On
来形成事件名称的前缀。 例如,Button
提供的Click
事件与OnClick
方法相关,该方法引发该事件(如另一个答案中所述)这里)。令人困惑的部分是
OnClick
方法 与OnClick
属性 没有任何关系> 问题所涉及的。通过编写不具有任何此类方法的控件,可以轻松证明
OnSomeEvent
方法在此无关紧要。 下面是一个简单用户控件的隐藏代码:它声明了一个
Foobar
事件。 (它实际上从未引发它,但这对于探索的目的来说并不重要。)它确实不定义OnFoobar
方法。 尽管如此,ASP.NET 非常乐意让我们在使用控件时使用OnSomeEvent
约定:事实上,它不仅乐意让我们这样做,而且实际上需要 它。 即使我的控件没有定义任何名为
OnFoobar
的成员(该事件仅调用Foobar
),如果我想,我也必须编写OnFoobar
附加我的.aspx
文件中的事件处理程序。 如果我只是在其中放置一个 Foobar 属性来尝试附加事件,则处理程序将永远不会运行。 (毫无帮助的是,当您这样做时,ASP.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.
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, theClick
event offered byButton
is related to anOnClick
method, which raises that event (as has already been stated in another answer here).The confusing part is that the
OnClick
method has nothing to do with theOnClick
attribute that the question concerns.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:This declares a
Foobar
event. (It never actually raises it, but that doesn't matter for the purposes of exploration.) It does not define anOnFoobar
method. Nevertheless, ASP.NET is perfectly happy for us to use theOnSomeEvent
convention when we use the control: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 justFoobar
—I have to writeOnFoobar
if I want to attach the event handler from my.aspx
file. If I just put aFoobar
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.)