tags:

views:

90

answers:

5

I have multiple forms in a web application that have buttons which are currently not functioning the way I want them to.

When the buttons are clicked, they will perform some behavior and then perform a Reponse.Redirect to some calculated location. The problem is the calculated location right now is wrong.

I'm thinking about replacing this redirect logic and trying to just hook into the back/forward functionality that web browsers have. The problem is, I don't know how to issue a

Response.PageBack() //back button equivalent on your standard browser

Does something like this exist in ASP.NET 2.0 or am I trying to do something that doesn't exist?

+2  A: 

How about adding the onclick client event to the button:

onclick="history.go(-1); return false;"
Darin Dimitrov
I believe that some server side processing of the web form needs to take place prior to redirecting the response, so @Joseph is looking for a server side solution. Correct?
James Conigliaro
+1 See history object on MSDN: http://tinyurl.com/lscpkp
JMP
@James That is correct, I have to do server side processing already, so I really need a server side solution. If I wasn't already processing something on the server side then I would definitely go this route, though!
Joseph
history.go(-1) is EXACTLY the behavior I want. Is there any way I can do this but still be able to do server side processing as well? I'm using an <asp:Button />
Joseph
history.go is a client side function. That's how browser's back button is implemented. There is no server side processing, though you could perform an AJAX call and if it succeeds you call history.go(-1).
Darin Dimitrov
@darin The problem is that I need this sequence. User clicks button -> do server side process -> initiate back button functionality. I'm not at liberty to use AJAX for the server side functionality, unfortunately, though that would be the route I would take if I was able to. I may have to do some kind of weird RegisterClientScript thing or something at the end of my server processing.
Joseph
A: 

It's not built in. You could build a list of pages the user has visited, in order - but that gets really hairy when you take into account multiples tabs/windows.

I doubt you'll be satisfied with your options. The back button in the browser is a security concern, so you aren't allowed to read the location in javascript - only navigate to some point in history (goo back 1 page, 3 pages, etc). So you could use javascript to send the user one page back, but if this is a shoppingcart type application, that may not be what you want.

Tom Ritter
A: 

There isn't a function that allows you to do this. The problem is that the history exists on the client, not on the server.

If you are only concerned with going ONE page back, then what you can do is when you render the page, take the referrer of the page that is being rendered and then set the event handler for the click of the button (in javascript) to navigate to that page.

You could also pass the value of the history.previous call in javascript back to your server, but the problem is that you have a huge security hole in that anyone can pass any url back.

casperOne
+1  A: 

The javascript version mentioned in another answer is probably better, but if you want to do it in the code behind, you can do:

Response.Redirect(Request.UrlReferrer.AbsoluteUri);

It's possible the referrer got stripped out, depending on how someone arrived at your page, but in that case it won't work any worse than the Back button of the browser would.

Sterno
A: 

Something like this should work reasonably well. Capture the referrer on the initial page load. When you do a postback, I believe the referrer is set to itself, so you can't rely on the Request.UrlReferrer during postback.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["previousPage"] = Request.UrlReferrer;
                // do other stuff as needed
            }

        }

        protected void Button_Click(object sender, EventArgs e)
        {
            // do a bunch of stuff

            Uri referrer = ViewState["previousPage"] as Uri;
            if (referrer != null)
                Response.Redirect(referrer.PathAndQuery);
        }
James Conigliaro