下面的代码示例演示如何在 Navigating 事件的处理程序中使用 Document 属性确定是否已填写了网页窗体。如果输入字段不包含任何值,则取消导航。
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());
    }
}

posted on 2012-11-06 09:26  速速  阅读(621)  评论(0编辑  收藏  举报