WebBrowser.DocumentText 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 WebBrowser 控制項中所顯示之網頁的 HTML 內容。
public:
property System::String ^ DocumentText { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Browsable(false)]
public string DocumentText { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.DocumentText : string with get, set
Public Property DocumentText As String
屬性值
所顯示之網頁的 HTML 文字;如果未載入任何文件,則為空字串 ("")。
- 屬性
例外狀況
這個 WebBrowser 執行個體已不再有效。
無法從基礎 ActiveX IWebBrowser2
控制項中擷取 WebBrowser
介面實作的參考。
範例
下列程式碼範例示範如何使用 DocumentText 屬性,以程式設計方式顯示您選擇的檔內容。 此範例會要求表單包含 WebBrowser 名為 的 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());
}
}
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>" & _
"</body></html>"
End Sub
Private Sub webBrowser1_Navigating( _
ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
Handles webBrowser1.Navigating
Dim document As System.Windows.Forms.HtmlDocument = _
webBrowser1.Document
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 " & _
e.Url.ToString())
End If
End Sub
備註
當您想要使用字串處理工具來操作控制項中顯示的 WebBrowser HTML 頁面內容時,請使用這個屬性。 例如,您可以使用這個屬性從資料庫載入頁面,或使用正則運算式來分析頁面。 當您設定此屬性時, WebBrowser 控制項會在載入指定的文字之前,自動巡覽至 about:blank URL。 這表示 Navigating 當您設定這個屬性時,就會發生 、 Navigated 和 DocumentCompleted 事件,而且 屬性的值 Url 不再有意義。
注意
此屬性包含目前檔的文字,即使已要求另一份檔也一樣。 如果您設定此屬性的值,然後再次擷取它,如果 WebBrowser 控制項沒有時間載入新內容,擷取的值可能會與設定的值不同。 您可以在事件處理常式中 DocumentCompleted 擷取新的值。 或者,您可以封鎖執行緒,直到檔載入為止,方法是在迴圈中呼叫 Thread.Sleep 方法,直到 DocumentText 屬性傳回您最初將它設定為的值為止。
若要以 存取網頁的內容, Stream 請使用 DocumentStream 屬性。 您也可以透過 屬性,使用 HTML 檔案物件模型 (DOM) Document 來存取頁面內容。