WebBrowser.Document WebBrowser.Document WebBrowser.Document WebBrowser.Document Property

定义

获取一个 HtmlDocument,它表示当前显示在 WebBrowser 控件中的网页。Gets an HtmlDocument representing the Web page currently displayed in the WebBrowser control.

public:
 property System::Windows::Forms::HtmlDocument ^ Document { System::Windows::Forms::HtmlDocument ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.HtmlDocument Document { get; }
member this.Document : System.Windows.Forms.HtmlDocument
Public ReadOnly Property Document As HtmlDocument

属性值

表示当前页的 HtmlDocument,如果未加载任何页,则为 nullAn HtmlDocument representing the current page, or null if no page is loaded.

异常

未能从基础 ActiveX IWebBrowser2 控件检索到对 WebBrowser 接口的实现的引用。A reference to an implementation of the IWebBrowser2 interface could not be retrieved from the underlying ActiveX WebBrowser control.

示例

下面的代码示例演示如何在Document Navigating事件的处理程序中使用属性来确定是否已填充网页窗体。The following code example demonstrates how to use the Document property in a handler for the Navigating event to determine whether a Web page form has been filled in. 如果输入字段不包含值, 则会取消导航。If the input field does not contain a value, the navigation is canceled.

此示例要求窗体包含一个名WebBrowser webBrowser1为的控件。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='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 文档对象模型 (DOM) 访问WebBrowser控件中显示的网页的内容, 请使用此属性。Use this property when you want to access the contents of a Web page displayed in the WebBrowser control through the HTML document object model (DOM). 例如, 当你想要在 Windows 窗体应用程序中使用基于 Web 的控件时, 这很有用。This is useful, for example, when you want to use Web-based controls in your Windows Forms application.

可以将此属性与ObjectForScripting属性结合使用, 以在WebBrowser控件中显示的网页与应用程序之间实现双向通信。You can use this property, in combination with the ObjectForScripting property, to implement two-way communication between a Web page displayed in the WebBrowser control and your application. HtmlDocument.InvokeScript使用方法可以从客户端应用程序代码调用网页中实现的脚本方法。Use the HtmlDocument.InvokeScript method to call script methods implemented in a Web page from your client application code. 脚本代码可通过window.external对象访问应用程序, 该对象是为主机访问提供的内置 DOM 对象, 并映射到为ObjectForScripting属性指定的对象。Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to an object that you specify for the ObjectForScripting property.

若要以字符串形式访问网页的内容, 请使用DocumentText属性。To access the contents of a Web page as a string, use the DocumentText property. 若要以形式Stream访问网页的内容, 请DocumentStream使用属性。To access the contents of a Web page as a Stream, use the DocumentStream property.

安全性

SecurityPermission
供直接调用方使用此控件。for immediate callers to use this control. 要求值: LinkDemand;命名权限集: FullTrustDemand value: LinkDemand; Named Permission Sets: FullTrust.

适用于

另请参阅