How to: Redirect Users to Another Page

JavaScript Editor jscript editor     Web designer 



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

    Visual BasicВ CopyCode imageCopy Code
    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
C#В CopyCode imageCopy Code
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.

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

See Also



JavaScript Editor jscript editor     Web designer