Edit

Share via


Accessing Unexposed Members on the Managed HTML Document Object Model

The managed HTML Document Object Model (DOM) contains a class called HtmlElement that exposes the properties, methods, and events that all HTML elements have in common. Sometimes, however, you will need to access members that the managed interface does not directly expose. This topic examines two ways for accessing unexposed members, including JScript and VBScript functions defined inside of a Web page.

Accessing Unexposed Members through Managed Interfaces

HtmlDocument and HtmlElement provide four methods that enable access to unexposed members. The following table shows the types and their corresponding methods.

Member Type Method(s)
Properties (HtmlElement) GetAttribute

SetAttribute
Methods InvokeMember
Events (HtmlDocument) AttachEventHandler

DetachEventHandler
Events (HtmlElement) AttachEventHandler

DetachEventHandler
Events (HtmlWindow) AttachEventHandler

DetachEventHandler

When you use these methods, it is assumed that you have an element of the correct underlying type. Suppose that you want to listen to the Submit event of a FORM element on an HTML page, so that you can perform some pre-processing on the FORM's values before the user submits them to the server. Ideally, if you have control over the HTML, you would define the FORM to have a unique ID attribute.

<HTML>  
  
    <HEAD>  
        <TITLE>Form Page</TITLE>  
    </HEAD>  
  
    <BODY>  
        <FORM ID="form1">  
             ... form fields defined here ...  
        </FORM>  
    </BODY>  
  
</HTML>  

After you load this page into the WebBrowser control, you can use the GetElementById method to retrieve the FORM at run time using form1 as the argument.

private void SubmitForm(String formName)
{
    HtmlElementCollection elems = null;
    HtmlElement elem = null;

    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        elems = doc.All.GetElementsByName(formName);
        if (elems != null && elems.Count > 0)
        {
            elem = elems[0];
            if (elem.TagName.Equals("FORM"))
            {
                elem.InvokeMember("Submit");
            }
        }
    }
}
Private Sub SubmitForm(ByVal FormName As String)
    Dim Elems As HtmlElementCollection
    Dim Elem As HtmlElement

    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Elems = .All.GetElementsByName(FormName)
            If (Not Elems Is Nothing And Elems.Count > 0) Then
                Elem = Elems(0)
                If (Elem.TagName.Equals("FORM")) Then
                    Elem.InvokeMember("Submit")
                End If
            End If
        End With
    End If
End Sub

Accessing Unmanaged Interfaces

You can also access unexposed members on the managed HTML DOM by using the unmanaged Component Object Model (COM) interfaces exposed by each DOM class. This is recommended if you have to make several calls against unexposed members, or if the unexposed members return other unmanaged interfaces not wrapped by the managed HTML DOM.

The following table shows all of the unmanaged interfaces exposed through the managed HTML DOM. Click on each link for an explanation of its usage and for example code.

Type Unmanaged Interface
HtmlDocument DomDocument
HtmlElement DomElement
HtmlWindow DomWindow
HtmlHistory DomHistory

The easiest way to use the COM interfaces is to add a reference to the unmanaged HTML DOM library (MSHTML.dll) from your application, although this is unsupported.

Accessing Script Functions

An HTML page can define one or more functions by using a scripting language such as JScript or VBScript. These functions are placed inside of a SCRIPT page in the page, and can be run on demand or in response to an event on the DOM.

You can call any script functions you define in an HTML page using the InvokeScript method. If the script method returns an HTML element, you can use a cast to convert this return result to an HtmlElement. For details and example code, see InvokeScript.

See also