Export (0) Print
Expand All

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.

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

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");
                    }
				}
			}
		}

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.

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. For more information, see Knowledge Base Article 934368.

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.

Community Additions

ADD
Show:
© 2014 Microsoft