Export (0) Print
Expand All

Redirecting Users to Another Page

You may want to redirect users from one Web Forms page to another page. You might do this to display a page that is matched to the user's browser capabilities or is written in the language that the user speaks.

There are two ways to redirect pages:

  • 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.
  • Using the browser. In this scenario, you send a command to the user's browser that causes the browser to fetch a different page. The advantage is that the browser's history is updated. The disadvantage is that this scenario performs an extra round trip, which can affect performance.

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

  • Call the Server.Transfer, passing it the name of the page to redirect to.
  • The following example shows how to redirect to another page.
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Server.Transfer("Webform2.aspx")
        End Sub
    
    // C#
    private void Button1_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("Webform2.aspx");
    }
    

    For details on sharing page context during a server transfer, see Passing Values Between Web Forms Pages.

To redirect users to another page from 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 redirect to.

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

    ' Visual Basic
    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#
    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");
    }
    

See Also

Passing Values Between Web Forms Pages | HttpResponse Class | HttpResponse.Redirect Method | Programming Web Forms

Show:
© 2014 Microsoft