How to Return to a Previous Page After a Server.Transfer
As an ASP.NET developer, you have the ability to speed up Web page transfers and possibly confuse some users at the same time. The Server.Transfer method, which causes a browser to display a new Web page, also leaves the original URL intact in the user's browser. If she was at site mysite.com and "mysite.com" still appears in her Address bar, she may not know that she is viewing a page that has a different URL. This may not be a problem if the user doesn’t care what appears in the Address bar. Using a simple trick, you can add a button to any ASPX page that causes the browser to return to the previous page.
Instructions
-
-
1
Launch Microsoft Visual Studio and open one of your C# ASP.NET projects.
-
2
Move to the Solution Explorer, double-click the project's startup form and then click "Design." The form appears in the Design window.
-
-
3
Add a button to the form and double-click the button. Visual Studio displays the button's click method that runs when users click the button.
-
4
Paste the code shown here into that method:
Server.Transfer("?");
Replace the question mark with the name of another ASPX form in your project. Return to the Solution Explorer and double-click that form.
-
5
Add a button to that form and double-click the button to view its click method.
-
6
Paste the code shown below into that method:
string previousPage = Request.UrlReferrer.ToString();
Response.Redirect(previousPage);The first line of code gets the URLReferrer property from the Request object and stores it in the previousPage variable. The next statement passes that variable to the Response.Redirect method. That method tells the browser to display the page whose URL matches the one stored in the previousPage method.
-
7
Press F5 to run the project. Your browser displays your first ASPX form. Click the form's button and the code executes the Server.Transfer method that causes the second form to appear in the browser.
-
8
Click the button that appears on that form. The browser takes you back to the previous page.
-
1