獲取一個(gè) HtmlDocument,它表示當前顯示在 WebBrowser 控件中的網(wǎng)頁(yè)。
命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)
| 異常 | 條件 |
|---|---|
| ObjectDisposedException | 此 WebBrowser 實(shí)例不再有效。 |
| InvalidOperationException | 未能從基礎 ActiveX WebBrowser 控件檢索到對 IWebBrowser2 接口的實(shí)現的引用。 |
如果要通過(guò) HTML 文檔對象模型 (DOM) 訪(fǎng)問(wèn)顯示在 WebBrowser 控件中的網(wǎng)頁(yè)的內容,則使用此屬性。 例如,如果要在 Windows 窗體應用程序中使用基于 Web 的控件,此屬性十分有用。
可以將此屬性與 ObjectForScripting 屬性組合使用,以便在 WebBrowser 控件中顯示的網(wǎng)頁(yè)與應用程序之間實(shí)現雙向通信。 使用 HtmlDocument.InvokeScript 方法從客戶(hù)端應用程序代碼調用網(wǎng)頁(yè)中實(shí)現的腳本方法。 腳本代碼可以通過(guò) window.external 對象訪(fǎng)問(wèn)應用程序,該對象是用于主機訪(fǎng)問(wèn)的內置 DOM 對象,它映射到為 ObjectForScripting 屬性指定的對象。
若要將網(wǎng)頁(yè)的內容作為字符串訪(fǎng)問(wèn),請使用 DocumentText 屬性。 若要將網(wǎng)頁(yè)的內容作為 Stream 訪(fǎng)問(wèn),請使用 DocumentStream 屬性。
下面的代碼示例演示如何使用 Navigating 事件處理程序中的 Document 屬性來(lái)確定網(wǎng)頁(yè)表單是否已經(jīng)填充。 如果輸入字段不包含值,則取消導航。
此示例要求窗體包含一個(gè)名為 webBrowser1 的 WebBrowser 控件。
private void Form1_Load(object sender, EventArgs e){webBrowser1.DocumentText ="<html><body>Please enter your name:<br/>" +"<input type='text' name='userName'/><br/>" +"<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());}}
- SecurityPermission
用于要使用此控件的直接調用方。要求值:LinkDemand;命名的權限集:FullTrust。
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 并不是對每個(gè)平臺的所有版本都提供支持。有關(guān)支持的版本的列表,請參見(jiàn).NET Framework 系統要求。




