Sys.EventHandlerList 类

更新:2007 年 11 月

将事件的名称用作键,并将关联的处理程序用作值,创建组件的客户端事件的字典。

命名空间:Sys

**继承:**无

var e = new Sys.EventHandlerList();

构造函数

名称

说明

Sys.EventHandlerList 构造函数

初始化 EventHandlerList 类的新实例。

成员

名称

说明

Sys.EventHandlerList.addHandler 方法

将处理程序附加到 EventHandlerList 实例中的指定事件,并且将该指定事件添加到列表中(如果该事件尚未存在于列表中)。

Sys.EventHandlerList getHandler 方法

返回一个单独的方法,可调用该方法来顺序调用指定事件的所有处理程序。

Sys.EventHandlerList.removeHandler 方法

EventHandlerList 实例中的指定事件移除事件处理程序。

备注

使用 EventHandlerList 类可处理自定义 ASP.NET AJAX 组件中的客户端事件。EventHandlerList 类为脚本块、组件或脚本资源文件中的事件及其处理程序提供一个中央引用位置。

说明:

此类仅在开发客户端组件时使用。它不用于组件开发范围以外的事件处理,也不用于 DOM 事件绑定。

若要引发事件,应调用 getHandler 方法,调用时将 id 参数设置为要引发的事件的标识符。然后调用 getHandler 返回的方法。这样将按顺序调用该事件的所有处理程序。

在派生自 Sys.Component 的类中,可以通过使用 Sys.Component 基类的 events 属性访问 EventHandlerList 的运行时实例。有关更多信息,请参见 Sys.Component.events 属性

示例

下面的示例演示如何在自定义 ASP.NET AJAX 控件中使用 EventHandlerList 类。有关如何创建自定义控件的信息,请参见创建自定义 AJAX 客户端控件,其中提供了本示例所基于的完整控件。

// Register namespace.
Type.registerNamespace("Demo");

Demo.HoverButton = function(element) {

    Demo.HoverButton.initializeBase(this, [element]);

    // Create delegates in the Constructor.
    this._clickDelegate = null;
}

Demo.HoverButton.prototype = {

    // Bind and unbind to click event.
    add_click: function(handler) {
        this.get_events().addHandler('click', handler);
    },
    remove_click: function(handler) {
        this.get_events().removeHandler('click', handler);
    },

    initialize: function() {
        var element = this.get_element();

        // Bind handler to delegate.
        if (this._clickDelegate === null) {
            this._clickDelegate = Function.createDelegate(this, this._clickHandler);
        }
        Sys.UI.DomEvent.addHandler(element, 'click', this._clickDelegate);
        Demo.HoverButton.callBaseMethod(this, 'initialize');
    },
    _clickHandler: function(event) {
        var h = this.get_events().getHandler('click');
        if (h) h(this, Sys.EventArgs.Empty);
    },

    // Release resources before control is disposed.
    dispose: function() {
        var element = this.get_element();
        if (this._clickDelegate) {
            Sys.UI.DomEvent.removeHandler(element, 'click', this._clickDelegate);
            delete this._clickDelegate;
        }
        Demo.HoverButton.callBaseMethod(this, 'dispose');
    }
}

// Register the class.
Demo.HoverButton.registerClass('Demo.HoverButton', Sys.UI.Control);

// Notify the ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>EventHandlerList Example</title>
</head>
<body>
<form id="form1" runat="server">
    <div id="ResultDisplay"></div>

        <asp:ScriptManager runat="server" ID="ScriptManager01">
            <scripts>
               <asp:ScriptReference Path="HoverButton.js" />
            </scripts>
        </asp:ScriptManager>

        <script type="text/javascript">
            var app = Sys.Application;
            // Add the handler function to the pageLoad event.
            app.add_load(applicationLoadHandler);

            function applicationLoadHandler(sender, args) {
                $create(
                    Demo.HoverButton, 
                    {element: {style: {borderWidth: "2px"}}},
                    // Bind the start function to the click event.
                    {click: start},
                    null,
                    $get('Button1')
                    );
            }

            function start(sender, args) {
               alert("The start function handled the HoverButton click event.");
            }
        </script>

        <button type="button" id="Button1" value="HoverButton">
            HoverButton
        </button>
</form>
</body>
</html>

请参见

参考

new 运算符

其他资源

语言参考