Partager via


How to: Redirect Users to Another Page

You might want to redirect users from one ASP.NET Web page to another Web page. For example, you might do this as part of a multi-page form.

There are a number of ways to redirect pages, such as the following:

  • By configuring the page to post to another page   In this scenario, the user clicks a button that you have configured to post to a different page. This scenario is useful for multi-page forms. However, it requires user interaction. For details, see Cross-Page Posting in ASP.NET Web Forms.

  • By dynamically using the browser   In this scenario, you send a command to the user's browser that causes the browser to retrieve a different page. This allows you to programmatically redirect to another page. However, the redirection causes a new request (an HTTP GET), and any post data from the source page is lost.

By dynamically using a server-side method   In this scenario, the server simply transfers the context to another page. The advantage is that you can share page context information between pages. The disadvantage is that the user's browser does not know about the transfer, so the browser's history is not updated. If the user refreshes the page, unexpected results can occur. For details about sharing page context during a server transfer, see How to: Pass Values Between ASP.NET Web Forms Pages.

To redirect a user to another page by using the browser

  1. Set the Response object's BufferOutput property to true.

  2. Call the Response object's Redirect method, passing it the URL of the page to which you want to redirect users.

    The following code example shows how to redirect a page based on the contents of a local variable, UserLanguage, which is set elsewhere.

    Response.BufferOutput = True
    If UserLanguage = "English" Then
        Response.Redirect("https://www.microsoft.com/gohere/look.htm")
    ElseIf UserLanguage = "Deutsch" Then
        Response.Redirect("https://www.microsoft.com/gohere/look_deu.htm")
    ElseIf UserLanguage = "Español" Then
        Response.Redirect("https://www.microsoft.com/gohere/look_esp.htm")
    End If
    
    Response.BufferOutput = true;
    if (UserLanguage == "English")
    {
        Response.Redirect("https://www.microsoft.com/gohere/look.htm");
    }
    else if (UserLanguage == "Deutsch")
    {
        Response.Redirect("https://www.microsoft.com/gohere/look_deu.htm");
    }
    else if (UserLanguage == "Español")
    {
        Response.Redirect("https://www.microsoft.com/gohere/look_esp.htm");
    }
    

To redirect users to another page by using a server-side method

  • Call the Transfer method, passing it the name of the page to which you want to redirect users.

    The following code example shows how to redirect to another page.

    Protected Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Server.Transfer("Page2.aspx", True)
    End Sub
    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("Page2.aspx", true);
    }
    

See Also

Tasks

How to: Pass Values Between ASP.NET Web Forms Pages

Concepts

Redirecting Users to Another Web Forms Page