Breaking News

Wednesday, January 13, 2010

Button click event being called automatically upon Page Refresh

Problem statement:
Button click event being called automatically upon Page Refresh (F5).

Bug:
When I press F5, I get duplicate records. That is, when I click on a save record button on my web page, and then if I refresh the page, that is hit the F5 key, the action repeats itself and another identical record is inserted in the database.

Reason:
That is in essence what a browser refresh functionality does; it repeats the last POST/GET action that was performed. And no wonder that we get to notice behavior only in such cases of duplicate record insertion (0:

By the way, if anyone of you did notice, Internet Explorer(and I am sure any other browser) does warn before adding a "duplicate record".




Workarounds:
1. Redirect to self:
Response.Redirect(Request.Url.AbsoluteUri);
2. Catch at the db level
3. Check through a sessioned variable
4. Detect browser's refresh button, here.


I would quote Terry Morton who touched the same topic starting with following comments:
Why on earth would the user be pressing the Refresh button?

Which you must have thought as well? well... she continued.
However, if you have been in application development for any length of time, you will know that end users have no limit to their "creative approaches" to application navigation. And they will INDEED press that Refresh button even though there is no logical reason to do so.

Happy refreshing! (0:

No comments:

Post a Comment

Designed By Published.. Blogger Templates