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
        End Sub
    // C#
    private void Button1_Click(object sender, System.EventArgs e)

    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
    ElseIf UserLanguage = "Deutsch" Then
    ElseIf UserLanguage = "Español" Then
    End If
    // C#
    Response.BufferOutput = true;
    if (UserLanguage == "English")
    else if (UserLanguage == "Deutsch")
    else if (UserLanguage == "Español")

See Also

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

© 2014 Microsoft