This documentation is archived and is not being maintained.

WebBrowser.DocumentText Property

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

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

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)

Not applicable.

Property Value

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

Exception typeCondition


This WebBrowser instance is no longer valid.


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 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.


This property contains the text of the current document, even if another document has been requested. If you set the value of this property and then immediately retrieve it again, the value retrieved may be different than the value set if the WebBrowser control has not had time to load the new content. You can retrieve the new value in a DocumentCompleted event handler. Alternatively, you can block the thread until the document is loaded by calling the System.Threading.Thread.Sleep method in a loop until the DocumentText property returns the value that you originally set it to.

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 2003 for Pocket PC, Windows Mobile 2003 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 a WebBrowser Control.

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=''>continue</a>" +
    webBrowser1.Navigating += 
        new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

private void webBrowser1_Navigating(object sender, 
    WebBrowserNavigatingEventArgs e)
    System.Windows.Forms.HtmlDocument document =

    if (document != null && document.All["userName"] != null && 
        e.Cancel = true;
            "You must enter your name before you can navigate to " +

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0