Freigeben über


HtmlObject.AttachEvent Method (String, EventHandler<HtmlEventArgs>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Attaches the specified .NET Framework event handler (EventHandler<TEventArgs>) to the specified event on the current Document Object Model (DOM) object.

Namespace:  System.Windows.Browser
Assembly:  System.Windows.Browser (in System.Windows.Browser.dll)

Syntax

'Declaration
Public Function AttachEvent ( _
    eventName As String, _
    handler As EventHandler(Of HtmlEventArgs) _
) As Boolean
public bool AttachEvent(
    string eventName,
    EventHandler<HtmlEventArgs> handler
)

Parameters

Return Value

Type: System.Boolean
true if the event was successfully attached; otherwise, false.

Exceptions

Exception Condition
ArgumentException

eventName is an empty string.

ArgumentNullException

eventName is nulla null reference (Nothing in Visual Basic).

-or-

handler is nulla null reference (Nothing in Visual Basic).

Remarks

The AttachEvent method returns a Boolean status value, which corresponds to the behavior in JavaScript.

The method returns an opaque reference (subscription cookie) to the event subscription. You can unsubscribe from the event by passing this reference to the DetachEvent method. If you attach the same .NET Framework delegate to an event multiple times, each event subscription results in a new subscription cookie, and the .NET Framework delegate is called multiple times when the DOM event is raised.

When the event is raised, the sender parameter for the event handler is a reference to the HtmlObject on which AttachEvent was originally called.

Events can be sent into managed code from DOM elements or HTML objects (such as the window object) in two ways:

  • An element can immediately send an event into managed code.

  • An element can trigger an event that bubbles up through the DOM hierarchy until it reaches an element that is capable of sending the event into managed code.

Therefore, the sender parameter value can be different from the Source property value.

Cross-browser event naming issues are handled as follows:

  • For Internet Explorer: If eventName does not start with the string "on", the string is prepended to the event name (for example, "click" is changed to "onclick"). This enables developers who want to use Firefox naming conventions to attach their events automatically in Internet Explorer.

  • For other browsers: If eventName starts with the string "on", the string is removed from the event name to support browsers that have different naming conventions. (For example, "onclick" is changed to "click, and "onblur" is changed to "blur".)

No action is taken if the event that is specified in eventName does not exist.

To obtain a reference to the DOM element that raised the event, use the AttachEvent(String, EventHandler<HtmlEventArgs>) method.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.