Loring Software, Inc

A Software Developer's Notebook
I searched far and wide for a simple way to disable a submit button in ASP.NET so that the user couldn't re-submit the form. There were a lot of complex solutions out there, but nothing really simple that wouldn't screw up client side form validation.

So, here it is folks. The really simple way to disable the button so the user can't submit the form more than once:

 <script type="text/javascript">
   var alreadyRan = false;
   function doGhost(iButton)
   {
       if (alreadyRan)
           iButton.disabled = true;
       alreadyRan = true;
   }
 </script>
 <asp:Button runat="server" ID="btnSubmit" OnClientClick="doGhost(this);" onclick="btnSubmit_Click" />

The trick is to not disable the button the first time they click the button.  That would not let the form submit.  But if the silly buggers want to try and click it a second time, only then do you disable the button (with the side benefit of not submitting the form).

I think OnClientClick might be a recent addition to the asp:Button.  If you're still back a version of .Net, you will have to add it as an attribute in the page load event:

    btnSubmit.Attributes.Add("onclick", "doGhost(this)");



Copyright © 2017 Loring Software, Inc. All Rights Reserved
Questions about what I am writing about? Email me