string referer = Request.UrlReferrer.ToString();
Using Request.UrlReferrer, we can get the Previous page URL of the current request. Previous Page information is available in the Request.UrlReferrer property only if user is redirected to the current page from some other page and is available in the !Page.IsPostBack of Page Load event of the form.
One thing to remember is that UrlReferrer will change to your pagename when you post back to the server.. so you should maybe store the value in ViewState on the first page load of the page if you are wanting to redirect the user to the previous page after a couple postbacks to the same page.
If you want to reach the previous page ,
ViewState is used for retaining values between multiple requests for the same page. This is the default method that the page uses to preserve page and control property values between round trips.
The situations where it does work include the following methods of a browser loading a URL:
- clicking on a straight HTML <a href> link;
- submitting a form, using POST or GET, from a submit button, <input type=image> or client-side script (form.submit())
The situations where it doesn't work:
- using Response.Redirect / Server.Transfer;
- clicking on a Favorite, History, or the recently-typed URLs list;
- clicking on 'Home' in IE's toolbar, or an item in IE's 'Links' toolbar;
- using HierMenus (details);
- typing the URL directly in the browser and hitting Enter or clicking 'Go';
- launching a clickable URL from an e-mail or MS Office document;
- using Response.AddHeader or <meta http-equiv=refresh> to redirect;
- loading the URL with XML