Redirecting to Another Page in ASP.NET , e.g website with different languages

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.

    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.

    protected void Button1_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("Page2.aspx", true);
    }

Comments