Export (0) Print
Expand All

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 Pages.

  • 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 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("http://www.microsoft.com/gohere/look.htm")
    ElseIf UserLanguage = "Deutsch" Then
        Response.Redirect("http://www.microsoft.com/gohere/look_deu.htm")
    ElseIf UserLanguage = "Español" Then
        Response.Redirect("http://www.microsoft.com/gohere/look_esp.htm")
    End If
    

    Response.BufferOutput = true;
    if (UserLanguage == "English")
    {
        Response.Redirect("http://www.microsoft.com/gohere/look.htm");
    }
    else if (UserLanguage == "Deutsch")
    {
        Response.Redirect("http://www.microsoft.com/gohere/look_deu.htm");
    }
    else if (UserLanguage == "Español")
    {
        Response.Redirect("http://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

Community Additions

ADD
Show:
© 2014 Microsoft