WebBrowser.Document 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 HtmlDocument,代表目前在 WebBrowser 控制項中顯示的網頁。
public:
property System::Windows::Forms::HtmlDocument ^ Document { System::Windows::Forms::HtmlDocument ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HtmlDocument Document { get; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HtmlDocument? Document { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Document : System.Windows.Forms.HtmlDocument
Public ReadOnly Property Document As HtmlDocument
屬性值
HtmlDocument,代表目前的網頁;如果未載入任何網頁,則為 null
。
- 屬性
例外狀況
這個 WebBrowser 執行個體已不再有效。
無法從基礎 ActiveX IWebBrowser2
控制項中擷取 WebBrowser
介面實作的參考。
範例
下列程式碼範例示範如何在 事件的處理常式 Navigating 中使用 Document 屬性,以判斷網頁表單是否已填入。 如果輸入欄位不包含值,則會取消導覽。
此範例會要求您的表單包含 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
備註
當您想要透過 HTML 檔案物件模型存取控制項中顯示的 WebBrowser 網頁內容時,請使用此屬性, (DOM) 。 例如,當您想要在 Windows Forms 應用程式中使用 Web 型控制項時,這會很有用。
您可以使用這個屬性搭配 ObjectForScripting 屬性,在控制項和應用程式中顯示的 WebBrowser 網頁之間實作雙向通訊。 HtmlDocument.InvokeScript使用 方法,從用戶端應用程式程式碼呼叫網頁中實作的腳本方法。 您的腳本程式碼可以透過 window.external
物件存取您的應用程式,這是提供給主機存取的內建 DOM 物件,以及對應至您為 ObjectForScripting 屬性指定的物件。
若要以字串的形式存取網頁的內容,請使用 DocumentText 屬性。 若要以 存取網頁 Stream 的內容,請使用 DocumentStream 屬性。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應