Saturday, March 24, 2012

shopping cart and browsers back button question

Buy.com has a great example of this. Go to buy.com and click on "purchase item" for 2 different items, so that they appear in the shopping cart. Now if you remove an item, the shopping cart updates, and if you click on the back button of your browser, the shopping cart is still correct with the one item. How is that done, and how can the same functionality be achieved with .Net? I understand the setting that prevents a page from being cashed, so that if i click back, I get the "Page Expired" error. But how can I set it up so that if I remove an item, and then click back, the item will no longer be there? I understand that this is a referal or page chashing trick, but can some one offer an explanation using .Net and C#(perfered).Hi, I am not sure about ther referal thing ... my cart is a bit diff ... I use session to store all the items ... so when an item is being removed, the cart is updated directly ... so as long as the cart is adding or deleting item, it always come out the correct item in the cart. My page doesn't go back to the same cart page when I hit the browser's Back button, it goes back to previous page (not cart page) ... so my pages are not involving any referal or something I guess. But hope this help.
You can make the ViewState of a control to true. By default its false.
Hi,

You can use the Request.UrlReferrer method, but you need to store the original UrlReferrer in viewstate otherwise the form will just redirect back to itself.

If Not Page.IsPostBack Then
ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()
End If

Then you can use it with an image back button like;

Private Sub ImgBack_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Response.Redirect(ViewState("ReferrerUrl").ToString())
End Sub

or at the end of any other sub

HTH

RG
Hi,

I have check the buy.com for this problem, thay are not using asp.net .
I think they are using JavaScript (Window.History) to perform this function.
Hey hbcontract,

so if I use the back button on your cart, where does IE point the browser to? Does it point to a cashed version of the previous page? If it does, then the code does not get executed on the prev page and room for errors opens up. How did you handle that situation?

Hey BhatiaWorld, you are prob right, will check the code on buy.com (but they prob include a js file).

Thanks for the responses everyone, try a solution based on your feedback.

Kommi
Actually, i just looked at the code and saw that Buy.com uses cookies to store what is in the shopping cart. Still dont understand how the cashed previous page is corrected with the contents of the shopping cart.
Hi, I used this and it worked fine, until I used a button to redirect to the new page rather than a hyperlink. Now, it does not know what page it was previously on when it gets to

ViewState("UrlReferrer") = Request.UrlReferrer.ToString()

Any way around this? Can I use the javascript (window.history) and how does that work?
Thanks for any help!
Greg
I used server.transfer rather that response.redirect and it worked fine.

0 comments:

Post a Comment