Two routine problems you have with a web application. Either the user clicks a button and they didn’t mean too or they click a button more than once, and meant to.
You can solve the first problem by asking for a confirmation.
Say you have a Delete button a web form. And you can the user to be prompted with a “Are you sure you want to do this?” confirmation box before proceeding. Simply put this code in the page_load.
btnSubmit.Attributes.Add(”onclick”, _
“return confirm(’Are you sure you want to delete this page? ” _
& “This cannot be undone.’);”)
The second problem I was having with some users, who shall remain unnamed, clicking the submit button a little too aggressively. Their machine being a little slow, they thought pressing the button again (and again) would make it go faster. This caused havok with one of my web applications by processing an order too many times. So I sought out to write a button that could be disabled on the client-side before the postback began.
I initially tried just calling this.disabled = true when the button was pressed. This caused a problem since a disabled button wouldn’t allow a postback. So like any good programmer, I Googled this and didn’t not have to look to far for like-minded people with a good solution.
In order for a disabled button to work you would have to force postback by adding
__doPostBack('btnProcess','');
to the onClick event. Of course this is better done by calling Page.GetPostBackEventReference(Me.btnProcess) on your Page_Load.
And to get validation to work properly the standard validation script
if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();
has to be replaced with
if (typeof(Page_ClientValidate) == 'function')
{ if (Page_ClientValidate() == false) { return false; }}
The final result for my solution was the following:
'This is the code for a Single-Click button
'Prevents a user from submitting a page more than once.
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
'This forces the page validation, if any, to execute.
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
'Changes the text of the button. Gives the user a processing message.
sb.Append("this.value = 'Processing...';")
'Prevent the button from being pressed a second time.
sb.Append("this.disabled = true;")
'Forces the page to postback.
sb.Append(Me.Page.GetPostBackEventReference(Me.btnProcess))
sb.Append(";")
Me.btnProcess.Attributes.Add("onclick", sb.ToString())
And of course if you want to combine these two functions you can simply use this code. This will ask the user for confirmation and only disable the button if the user clicks ‘yes’.
'This is the code for a Single-Click button
'Prevents a user from submitting a page more than once.
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
'Confirm user action before processing.
sb.Append("if (confirm('Are you sure you want to process this shipment? " _
& "This cannot be undone.') == false) { return false; } ")
'This forces the page validation, if any, to execute.
sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")
sb.Append("if (Page_ClientValidate() == false) { return false; }} ")
'Changes the text of the button. Gives the user a processing message.
sb.Append("this.value = 'Processing...';")
'Prevent the button from being pressed a second time.
sb.Append("this.disabled = true;")
'Forces the page to postback.
sb.Append(Me.Page.GetPostBackEventReference(Me.btnProcess))
sb.Append(";")
Me.btnProcess.Attributes.Add("onclick", sb.ToString())
And that’s it. This should be on every web application where multiple submits could cause a problem.