Sign in to follow this  
jen6677

[.net] beginner Q: how do you handle "refresh" problems with asp.net?

Recommended Posts

I have a web form that has 1 button(add to cart). If the user clicks the button then obviously the button's onClick method is called. Inside that method I update the database and I set the "add to cart" button to Not Visible The problem occurs when the user refreshes the page after the "add to cart" button has clicked. The item is added to the cart twice. I thought that once refresh was clicked on the browser the form's pageLoad function would be called. This doesnt look like the case. How do I stop the database being updated by refreshing from the browser. Thanks

Share this post


Link to post
Share on other sites
If the user refreshes the page and reposts a POST with a still-valid viewstate, that counts the same as if they had just hit back and hit the button again.

If you're storing the basket in a session variable (or a DB, etc), it's going to get the product added twice.

The Page_Load event ALWAYS gets called when the page is loaded, before any of the other events get fired. The page propery IsPostBack gets set if the page was a postback.

Normally it's common to see


private void Page_Load(Event e) {
if (! IsPostBack) {
// stuff
}
}


You can't really stop this from happening. If the user resubmits the page, it's their own silly fault.

Of course you could redirect the browser after you've added an item, instead of showing the basket as a post result - but it won't really solve the problem. Using "back" and resubmitting will still add the product again.

Mark

Share this post


Link to post
Share on other sites
Well, there are 2 tricks:
1. Use Server.Redirect(...) to forward the user to some sort of confirmation page. (Many sites do this)
2. Set a user (session) variable to mark the action. then first check this mark before acting upon the submit.

The IsPostback isn't meant to be used like this. Many times it won't even work because the submit won't be the first postback.

Cheers

Share this post


Link to post
Share on other sites
Quote:
Original post by ernow
The IsPostback isn't meant to be used like this. Many times it won't even work because the submit won't be the first postback.

Well ... to make a slight correction ... The page_load event will *always* be called before any event handlers. no matter what. By the time any click event handler is executed, IsPostBack will == true. So pretty much the only situation that could use this is loading initial data in the form ... any manner of persistence between postbacks is usually done by viewstate.

The other poster who suggested setting a session variable was right ... that's about the only way of protecting the user from themselves.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this