WebBrowser.DocumentText WebBrowser.DocumentText WebBrowser.DocumentText WebBrowser.DocumentText Property


取得或設定 WebBrowser 控制項中所顯示之網頁的 HTML 內容。Gets or sets the HTML contents of the page displayed in the WebBrowser control.

 property System::String ^ DocumentText { System::String ^ get(); void set(System::String ^ value); };
public string DocumentText { get; set; }
member this.DocumentText : string with get, set
Public Property DocumentText As String


所顯示之網頁的 HTML 文字;如果未載入任何文件,則為空字串 ("")。The HTML text of the displayed page, or the empty string ("") if no document is loaded.


這個 WebBrowser 執行個體已不再有效。This WebBrowser instance is no longer valid.

無法從基礎 ActiveX IWebBrowser2 控制項中擷取 WebBrowser 介面實作的參考。A reference to an implementation of the IWebBrowser2 interface could not be retrieved from the underlying ActiveX WebBrowser control.


下列程式碼範例示範如何使用DocumentText以程式設計方式顯示您所選擇的文件內容的屬性。The following code example demonstrates how to use the DocumentText property to programmatically display document content of your choosing. 這個範例需要您的表單包含WebBrowser控制項稱為webBrowser1This 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>" +
    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 " +
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Me.Load

    webBrowser1.DocumentText = _
        "<html><body>Please enter your name:<br/>" & _
        "<input type='text' name='userName'/><br/>" & _
        "<a href='http://www.microsoft.com'>continue</a>" & _

End Sub

Private Sub webBrowser1_Navigating( _
    ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
    Handles webBrowser1.Navigating

    Dim document As System.Windows.Forms.HtmlDocument = _
    If document IsNot Nothing And _
        document.All("userName") IsNot Nothing And _
        String.IsNullOrEmpty( _
        document.All("userName").GetAttribute("value")) Then

        e.Cancel = True
        MsgBox("You must enter your name before you can navigate to " & _
    End If

End Sub


使用這個屬性,當您想要操作的內容中所顯示的 HTML 網頁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. 當您設定此屬性,WebBrowser控制項自動巡覽至相關: 空白 URL,然後再載入指定的文字。When you set this property, the WebBrowser control automatically navigates to the about:blank URL before loading the specified text. 這表示NavigatingNavigated,並DocumentCompleted當您設定此屬性,以及的值時,就會發生事件Url屬性不再有意義。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. 如果您設定這個屬性的值,並再次立即擷取它,擷取的值可能會不同的值設定如果WebBrowser控制項還沒有時間來載入新的內容。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. 您可以擷取中的新值DocumentCompleted事件處理常式。You can retrieve the new value in a DocumentCompleted event handler. 或者,您可以封鎖執行緒,直到呼叫載入文件Thread.Sleep方法,在迴圈中,直到DocumentText屬性會傳回原本要設定的值。Alternatively, you can block the thread until the document is loaded by calling the Thread.Sleep method in a loop until the DocumentText property returns the value that you originally set it to.

若要存取的網頁作為內容Stream,使用DocumentStream屬性。To access the contents of a Web page as a Stream, use the DocumentStream property. 您也可以透過存取使用 HTML 文件物件模型 (DOM) 的頁面內容Document屬性。You can also access the page contents using the HTML document object model (DOM) through the Document property.


使用此控制項的立即呼叫端。for immediate callers to use this control. 要求值: LinkDemand;[NIB:具名使用權限集合](https://msdn.microsoft.com/library/08250d67-c99d-4ab0-8d2b-b0e12019f6e3): FullTrustDemand value: LinkDemand; [NIB: Named Permission Sets](https://msdn.microsoft.com/library/08250d67-c99d-4ab0-8d2b-b0e12019f6e3): FullTrust.