Share via

HtmlObject.AttachEvent Method (String, EventHandler)

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

Attaches the specified .NET Framework event handler (EventHandler) 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)


Public Function AttachEvent ( _
    eventName As String, _
    handler As EventHandler _
) As Boolean
public bool AttachEvent(
    string eventName,
    EventHandler handler


Return Value

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


Exception Condition

eventName is an empty string.


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


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


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

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


Supported in: 5, 4, 3


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