Thursday, August 03, 2006

There comes a time when you have to develop a page which allows its users to go back to the page they came from. Keeping in mind the statlessnes of the web applications, it is hard to tell where our users came from since every page is (or at least should be) working on its own. Suppose that on one of the pages we want to put a kind of “Cancel” button which takes users back. One possible way of doing it would be to use some client-side JavaScript such as history.back(). What if for some reason the use of JavaScript is inacceptable?

We are left with the server-side code. But there is a problem. On the server, there is no way of telling the previous page. We have to create our own solution. There are couple of possibilities here. You can use a Session or Profile object and every time a page is loaded put some information there so we have a way of checking where the user was before. You can even use a Stack object to make it possible to drill down and up as you like. This solution has however an impact on the server resources (memory occupied by the session object). Another solution uses the query url to pass the returnUrl address of page from which the users came. In fact this pattern is used every time you try to access a page which requires authorization and you are redirected to a login page. This way you save some valuable server resources and make your solution a lot more scalable.

kick it on dotnetkicks.com

Sunday, August 13, 2006 11:10:15 AM (Central European Standard Time, UTC+01:00)
You can just use the Server.Variables["referer"] or Request.UrlReferrer to find out what the previous page was.
Sunday, August 13, 2006 9:59:33 PM (Central European Standard Time, UTC+01:00)
A nice suggestion and a useful one. What if you want to have it work for a path of n-pages? I find that returnUrl works for 2 pages (maybe 3) and then something goes wrong when returning to the first page.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview