How to Really write a Server Control in ASP.Net
- November 13, 2013
Find Missing Dependency DLLs on Win 7
- March 5, 2012
Nullable TryParse
- May 26, 2011
Diff SQL Server Stored Procedures, November 15, 2010
Reporting Services Extranet Access, March 16, 2010
Case of the missing WaitCursor, January 7, 2009
Simple Submit Button Disable, December 9, 2009
An Efficient Memory Stream, September 29, 2009
Approach Plate Download - May 14, 2009
WPF Binding - Async Web Services - April 10, 2009
Developing the Blog
- April 4, 2009
|
|
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)");
|