为什么WebControl事件的前缀为“On”? [英] Why do WebControl events have the prefix of "On"?

查看:161
本文介绍了为什么WebControl事件的前缀为“On”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完全了解WebForm事件模型(而不是页面生命周期,但是如何在.aspx或.ascx文件中以声明方式指定事件的连接方式。



以Button控件为例,它有一个Click事件,可以连接到代码隐藏,但它在.aspx / .ascx文件中有一个OnClick事件。



我使用.NET Reflector,而Button控件有一个PROTECTED OnClick方法,但这不应该由.aspx / .ascx分配,除非我缺少一些东西。



有没有人知道为什么添加开前缀?



只是为了澄清一点:我明白命名约定工作,我想知道.aspx / .ascx中的OnClick如何被翻译成.Click + = new EventHandler(blahName);如果我创建一个ControlChanged EventHandler,我需要做任何事情特别的是让OnControlChanged在.aspx / .ascx文件中有效显示?

解决案例

这不仅仅是一个命名约定,因为用户控件中的事件会在声明语法中自动获得On前缀。



例如,有一个UserControl声明一个ProjectSelected事件。要声明地添加一个处理程序,我设置了OnProjectSelected属性。



UserControl:

  public event EventHandler< ProjectSelectedEventArgs> ProjectSelected; 

声明地添加句柄:

 < user:ProjectList id =uxProjectListrunat =server
OnProjectSelected =uxProjectList_ProjectSelected/>

在后面的代码中添加处理程序:

  uxProjectList.ProjectSelected + = uxProjectList_ProjectSelected; 

这让我困惑了我两次,当我无法弄清楚为什么这个事件是当我命名事件OnProjectSelected并且属性变为OnOnProjectSelected时,再次声明性地提供。


I'm trying to fully understand the WebForm event model (not the page lifecycle, but how the events are wired up when specified declaratively in the .aspx or .ascx files.

Take the Button control for example. It has a Click event that you can wire to in the code-behind, but it has an "OnClick" event in the .aspx/.ascx file.

I used the .NET Reflector and the Button control has a PROTECTED OnClick method, but that shouldn't be available to be assigned by the .aspx/.ascx. Unless I'm missing something.

Does anyone know why the "On" prefix is added?

Just to clarify a bit: I understand the naming convention works. I'd like to know how the "OnClick" in the .aspx/.ascx gets translated into .Click += new EventHandler(blahName); I.e. if I create a ControlChanged EventHandler, do I need to do anything special to get the OnControlChanged to show up validly in the .aspx/.ascx file?

解决方案

It's more than a naming convention because events in user controls automatically get the "On" prefix in the declarative syntax.

For example, I have a UserControl that declares a ProjectSelected event. To add a handler declaratively, I set the OnProjectSelected attribute.

UserControl:

        public event EventHandler<ProjectSelectedEventArgs> ProjectSelected;

Adding handler declaratively:

        <user:ProjectList id="uxProjectList" runat="server"
            OnProjectSelected="uxProjectList_ProjectSelected" />

Adding handler in code behind:

        uxProjectList.ProjectSelected += uxProjectList_ProjectSelected;

This confused the hell out of me twice, once when I couldn't figure out why the event wasn't available declaratively, and again when I named the event "OnProjectSelected" and the attribute became "OnOnProjectSelected".

这篇关于为什么WebControl事件的前缀为“On”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆