WebBrowser.Navigating WebBrowser.Navigating WebBrowser.Navigating WebBrowser.Navigating Event

定义

WebBrowser 控件导航到新文档之前发生。Occurs before the WebBrowser control navigates to a new document.

public:
 event System::Windows::Forms::WebBrowserNavigatingEventHandler ^ Navigating;
public event System.Windows.Forms.WebBrowserNavigatingEventHandler Navigating;
member this.Navigating : System.Windows.Forms.WebBrowserNavigatingEventHandler 
Public Event Navigating As WebBrowserNavigatingEventHandler 

示例

下面的代码示例演示如何使用Navigating事件的处理程序在未填充网页窗体时取消导航。The following code example demonstrates how to use a handler for the Navigating event to cancel navigation when a Web page form has not been filled in. Document属性用于确定窗体输入字段是否包含值。The Document property is used to determine whether the form input field contains a value.

此示例要求您的窗体包含WebBrowser一个名webBrowser1为的控件, 并且您的ComVisibleAttribute窗体类具有可供 COM 使用的控件。This example requires that your form contains a WebBrowser control called webBrowser1 and that your form class has a ComVisibleAttribute making it accessible to COM.

有关可以将以下代码粘贴到的完整代码示例, 请参阅如何:将 Web 浏览器功能添加到 Windows 窗体应用程序。For a complete code example that you can paste the following code into, see How to: Add Web Browser Capabilities to a Windows Forms Application.

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

注解

WebBrowser设置以下属性之一或调用方法时, 控件将导航到新文档:The WebBrowser control navigates to a new document whenever one of the following properties is set or methods is called:

如果尚未满足某些Navigating条件 (例如, 当用户未完全填写表单时), 则可以处理事件以取消导航。You can handle the Navigating event to cancel navigation if certain conditions have not been met, for example, when the user has not completely filled out a form. 若要取消导航, 请Cancel将传递到WebBrowserNavigatingEventArgs事件处理程序的对象的属性true设置为。To cancel navigation, set the Cancel property of the WebBrowserNavigatingEventArgs object passed to the event handler to true. 还可以使用此对象通过WebBrowserNavigatingEventArgs.Url属性检索新文档的 URL。You can also use this object to retrieve the URL of the new document through the WebBrowserNavigatingEventArgs.Url property. 如果新文档将显示在网页框架中, 则可以通过WebBrowserNavigatingEventArgs.TargetFrameName属性检索框架的名称。If the new document will be displayed in a Web page frame, you can retrieve the name of the frame through the WebBrowserNavigatingEventArgs.TargetFrameName property.

处理事件, 以便在WebBrowser控件完成导航并开始在新位置加载文档时接收通知。 NavigatedHandle the Navigated event to receive notification when the WebBrowser control finishes navigation and has begun loading the document at the new location. 处理事件, 以便在WebBrowser控件完成加载新文档时接收通知。 DocumentCompletedHandle the DocumentCompleted event to receive notification when the WebBrowser control finishes loading the new document.

有关处理事件的详细信息, 请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

安全性

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

适用于

另请参阅