This documentation is archived and is not being maintained.

WebBrowser.DocumentText Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the HTML contents of the page displayed in the WebBrowser control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public string DocumentText { get; set; }
/** @property */
public String get_DocumentText ()

/** @property */
public void set_DocumentText (String value)

public function get DocumentText () : String

public function set DocumentText (value : String)

Property Value

The HTML text of the displayed page, or the empty string ("") if no document is loaded.

Exception typeCondition

ObjectDisposedException

This WebBrowser instance is no longer valid.

InvalidOperationException

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

Use this property when you want to manipulate the contents of an HTML page displayed in the WebBrowser control using string processing tools. You can use this property, for example, to load pages from a database or to analyze pages using regular expressions. When you set this property, the WebBrowser control automatically navigates to the about:blank URL before loading the specified text. This means that the Navigating, Navigated, and DocumentCompleted events occur when you set this property, and the value of the Url property is no longer meaningful.

To access the contents of a Web page as a Stream, use the DocumentStream property. You can also access the page contents using the HTML document object model (DOM) through the Document property.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The .NET Compact Framework does not support using DocumentText to get the content of a Web page, but you can use it to present HTML to your users, such as to provide links and a simple HTML form. For a code example, see How to: Use the WebBrowser Control in the .NET Compact Framework.

The following code example demonstrates how to use the DocumentText property to programmatically display document content of your choosing. 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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Show: