HeartattacK

Refresh Button and Double Insert / Updates

I saw a few questions on the forums about double posting of data because of users using the refresh buttons in the browser.

Ideally, I'd want to have my insert / update mechanisms be done by AJAX calls to a web service. While this will work nicely regardless of back / refresh buttons being pressed, there is a necessary overhead, and a lot of people (including me, in a hurry) find the default features of a gridview very attractive. Granted, I could just put the gridview in an update panel and be done with it, but there's a quick fix in case you're not using AJAX:

After the insert/update is done, if it was successful, just do a Response.Redirect to the same page.

 UPDATE: As pointed out by the always helpful anas, tmorton has an excellent and in depth article on this topic. She presents various commonly used methods that DON'T WORK, and also some that do. It seems like the only viable options are doing what I suggested above, or actually changing the db so that it doesn't allow duplicates by using unique constraints (which isn't always what we want) or to check the db before indexing to see if the insert/update does something we don't want. The last option is ideally the best, but would require quite a bit of code for each and every insert. I'm as surprised as she was that the last method achieved the best performance in her tests. I could be wrong, but it seemed that her tests were made taking into account that users would be hitting the refresh button after insert most, if not all of the time. I wonder if the last method would be as fast compared to the others if most inserts were just inserts and no refresh button were clicked (as would be the most expected case). If the checks are always made, then they would be redundant only when the checks weren't needed. And most of the time, they would not be needed.

The problem with the method I suggested has the problem that the ViewState is cleared for the controls. If there is no problem with that, then it can work very well, while maintaining a lot of simplicity. If we wanted some controls to retain their values, we could easily save them in Session, and retrieve them after redirect. This would add complexity, but nowhere near the complexity needed to make all the checks everytime. Still, ideally, we should all be checking the db everytime. But when we want a quick solution, the redirect would work great.

Of course, on my pages, I always use AJAX to handle the inserts, whether in an updatepanel or by calling a webservice. That, IMO is the best way and it doesn't have to deal with that pesky double posting thing at all.

Posted: Aug 02 2008, 02:33 AM by HeartattacK | with 1 comment(s)
Filed under:

Comments

No Comments