views:

71

answers:

6

Hi there,

Simple one here... is there a clean way of preventing a user from double-clicking a button in a web form and thus causing duplicate events to fire?

If I had a comment form for example and the user types in "this is my comment" and clicks submit, the comment is shown below... however if they double-click, triple-click or just go nuts on the keyboard they can cause multiple versions to be posted.

Client-side I could quite easily disable the button onclick - but I prefer server-side solutions to things like this :)

Is there a postback timeout per viewstate that can be set for example?

Thanks

+1  A: 

Set a session variable when the user enters the page like Session["FormXYZSubmitted"]=false. When the form is submitted check that variable like

if((bool) Session["FormXYZSubmitted"] == false) {
   // save to db
   Session["FormXYZSubmitted"] = true;
}
Zafer
you can do it on button click event as well to be precise
Anil
I did not include that but it's correct.
Zafer
A: 

Hi,

I have had the same scenario. The solution of one of my coworkers was to implement a kind of Timer in Javascript, to avoid considering the second click as a click.

Hope that helps,

Ramon Araujo
+2  A: 

With jQuery you can make use of the one event.

Another interesting read is this: Build Your ASP.NET Pages on a Richer Bedrock.

XIII
+1  A: 

I dont think that you should be loading the server for trivial tasks like these. You could try some thing jquery UI blocking solution like this one. Microsoft Ajax toolkit should also have some control which does the same. I had used it a long time ago, cant seem to recall the control name though.

Vinay B R
Fair point, client side it is!
beardtwizzle
A: 

Disable the button on click, utilize jquery or microsoft ajax toolkit.

alfdev
A: 

Depending on how important this is to you, could create an array of one time GUID's which you remove from the array once the update has been processed (ie posted back in viewstate/hidden field)

If the guid is not in the array on postback, the request is invalid.

Substitute database table for array in a clustered environment.

James Westgate