Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent double clicking asp.net button

Tags:

I realise this question has been asked but none of the answers worked for my project.

I have a button that when clicked calls an API, so there is a 1 second delay.

I have tried several things nothing works.

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

Even that does nothing.

like image 582
WhiteSpider Avatar asked Aug 08 '14 09:08

WhiteSpider


2 Answers

Prevent Double Click .Please add below code in your aspx page.

<script type="text/javascript" language="javascript">     Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);    function BeginRequestHandler(sender, args) { var oControl = args.get_postBackElement(); oControl.disabled = true; }  </script> 
like image 135
Deepak Joshi Avatar answered Nov 12 '22 01:11

Deepak Joshi


This solution is simple and effective. On your button include this code:

OnClientClick="return CheckDouble();" 

And wherever you want your JavaScript - e.g. At the bottom of your page:

<script type="text/javascript">    var submit = 0;    function CheckDouble() {      if (++submit > 1) {      alert('This sometimes takes a few seconds - please be patient.');      return false;    }  }  </script> 
like image 33
Antony D Avatar answered Nov 11 '22 23:11

Antony D