WebBrowser.Document 属性

定义

获取一个 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 文档对象模型 (DOM) 访问控件中显示的 WebBrowser 网页的内容,请使用此属性。 例如,如果要在Windows 窗体应用程序中使用基于 Web 的控件,这非常有用。

可以将此属性与 ObjectForScripting 属性结合使用,在控件中显示的 WebBrowser 网页与应用程序之间实现双向通信。 HtmlDocument.InvokeScript使用 方法从客户端应用程序代码调用在网页中实现的脚本方法。 脚本代码可以通过 对象(为主机访问提供的内置 DOM 对象)访问应用程序 window.external ,该对象映射到为 ObjectForScripting 属性指定的 对象。

若要以字符串形式访问网页的内容,请使用 DocumentText 属性。 若要以 的形式 Stream访问网页的内容,请使用 DocumentStream 属性。

适用于

另请参阅