Redigeeri

Jagamisviis:


WebBrowser.Navigating Event

Definition

Occurs before the WebBrowser control navigates to a new document.

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

Event Type

Examples

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. The Document property is used to determine whether the form input field contains a value.

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.

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

Remarks

The WebBrowser control navigates to a new document whenever one of the following properties is set or methods is called:

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. To cancel navigation, set the Cancel property of the WebBrowserNavigatingEventArgs object passed to the event handler to true. You can also use this object to retrieve the URL of the new document through the WebBrowserNavigatingEventArgs.Url property. 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.

Handle the Navigated event to receive notification when the WebBrowser control finishes navigation and has begun loading the document at the new location. Handle 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.

Applies to

See also