Export (0) Print
Expand All

WebBrowser.Document Property

Gets an HtmlDocument representing the Web page currently displayed in the WebBrowser control.

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

[BrowsableAttribute(false)]
public HtmlDocument Document { get; }

Property Value

Type: System.Windows.Forms.HtmlDocument
An HtmlDocument representing the current page, or null if no page is loaded.

ExceptionCondition
ObjectDisposedException

This WebBrowser instance is no longer valid.

InvalidOperationException

A reference to an implementation of the IWebBrowser2 interface could not be retrieved from the underlying ActiveX WebBrowser control.

Use this property when you want to access the contents of a Web page displayed in the WebBrowser control through the HTML document object model (DOM). This is useful, for example, when you want to use Web-based controls in your Windows Forms application.

You can use this property, in combination with the ObjectForScripting property, to implement two-way communication between a Web page displayed in the WebBrowser control and your application. Use the HtmlDocument.InvokeScript method to call script methods implemented in a Web page from your client application code. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to an object that you specify for the ObjectForScripting property.

To access the contents of a Web page as a string, use the DocumentText property. To access the contents of a Web page as a Stream, use the DocumentStream property.

The following code example demonstrates how to use the Document property in a handler for the Navigating event to determine whether a Web page form has been filled in. If the input field does not contain a value, the navigation is canceled.

This example requires that your form contains a WebBrowser control called webBrowser1.

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText =
        "<html><body>Please enter your name:<br/>" +
        "<input type='text' name='userName'/><br/>" +
        "<a href='http://www.microsoft.com'>continue</a>" +
        "</body></html>";
    webBrowser1.Navigating += 
        new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}

private void webBrowser1_Navigating(object sender, 
    WebBrowserNavigatingEventArgs e)
{
    System.Windows.Forms.HtmlDocument document =
        this.webBrowser1.Document;

    if (document != null && document.All["userName"] != null && 
        String.IsNullOrEmpty(
        document.All["userName"].GetAttribute("value")))
    {
        e.Cancel = true;
        System.Windows.Forms.MessageBox.Show(
            "You must enter your name before you can navigate to " +
            e.Url.ToString());
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft