Monday, July 4, 2011

ASP.NET: Prevent button double-click

Problem

How to prevent button double-click on ASP.NET page

Solution

Add following code to Page_load. This code prevents button double-click on client-side but still executes code at server-side:

VB.Net

btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, Nothing) + ";")

C#

btnSave.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");

35 comments:

  1. it gives this error:
    "Microsoft JScript runtime error: Sys.ParameterCountException: Parameter count mismatch."

    ReplyDelete
  2. Its Working nice for me, But problem crops up when I have RequiredFieldValidators on my Page

    ReplyDelete
  3. same error:"Microsoft JScript runtime error: Sys.ParameterCountException: Parameter count mismatch."

    ReplyDelete
  4. Thanks. It Works great for me.

    ReplyDelete
  5. good work... it worked for me
    thanks.

    ReplyDelete
  6. thanks. it works for me...

    ReplyDelete
  7. Works!!!

    For the error thing: Change it "onclick" to "onClientClick" (basically system complaining about multiple object for "onclick" property)

    For adding the validation, can be like this: (in VB.NET)
    btnEvaluateNext.Attributes.Add("onclientclick", "if(Page_ClientValidate('ValidationGroup')){this.disabled = true; + ClientScript.GetPostBackEventReference(btnEvaluateNext, Nothing) + ";}")

    and actually without the __dopostback thing it will still work:
    btnEvaluateNext.Attributes.Add("onclientclick", "if(Page_ClientValidate('vgRating')){this.disabled = true; }")

    ReplyDelete
  8. Sorry, it works last Friday but not now.. I don't even change anything. Weird and I just get rid of this disable stuff...

    ReplyDelete
  9. Really helpful .. Thanks tommi

    ReplyDelete
  10. Awe a simple answer to an aggravating problem. THANKS!

    ReplyDelete
  11. i get err :Uncaught ReferenceError: ClientScript is not defined

    ReplyDelete
  12. I have same, but issue is still I need to click twice on link button.
    Well I have tried many things, but could not resolve the issue. I have more than 2 update panel and link buttons are on the update panel, but update panel are not place within update panel. Any help or suggestion are highly appreciated.

    ReplyDelete
  13. It works for me well. In my code I have several validations. But After implementing below. If I leave any mandatory field and click on Save button. The validation message box pops up twice. first it shows a message box "Please fill the fields" and after clicking on "OK" the message is displayed again....Any idea how to rectify this?

    ButtonSave.Attributes.Add("onclick", "if(Page_ClientValidate('ValidatePage')){this.disabled = true;} " + ClientScript.GetPostBackEventReference(ButtonSave, null) + ";");

    ReplyDelete
  14. works great, thanks !!

    ReplyDelete
  15. Thanks! worked for me :)

    ReplyDelete
  16. Thanks bro ...
    worked for me too...
    good job.

    ReplyDelete
  17. Thank you very much, its working

    ReplyDelete
  18. Thanks work fine

    ReplyDelete
  19. Awesome Works Great!! I have been searching for past two days for an answer and wouldn't you know it would be a one liner that would do the trick. Thanks Much!

    ReplyDelete
  20. Worked for me (VB.net)! Thank you!!

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. I've seen other solutions and yours seems easiest. Working good.

    ReplyDelete
  23. In vb.net there shouldn't be a plus(+) sign, it should be an ampersand-->&.

    You guys could be getting errors because of that.

    ReplyDelete
  24. Thanks.
    It's working for me ...

    ReplyDelete
  25. Thanks.
    It works for me

    ReplyDelete
  26. btnSave.Attributes.Add("onclick",ClientScript.GetPostBackEventReference(btnSave, "")+ ";this.value='Processing...';this.disabled = true; this.style.display='block';");
    If i am using this one how can i perform client side validation?

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. I found this article easy to understand and very helpful. Can’t wait to see the other posts. Thank you for sharing!

    ReplyDelete