Introduction
Back in the days of classic ASP, when we wanted to prevent the end user from repeatedly mashing the submit button, we would disable the button with JavaScript. But with ASP.NET, it's not as simple as that anymore. When you disable any form element client side, you essentially disable it server side as well since its name value pair will not be sent with the form post. The drawback from this is that the button_click
event is now rendered useless. It will never be raised!
I have searched the ASP.NET forums over for an elegant solution for this problem and I have seen a couple of nice work arounds but nothing that gives the true effect of rendering the button disabled after being clicked. So I came up with my own. Enter the ClickOnceButton
server control.
The server control behaves exactly like the intrinsic button control with the exception of two new properties: DisableAfterClick
and DisabledText
.
When DisableAfterClick
is true, the button will be disabled client side after click. If DisabledText
is set, the text of the button will be set to the DisabledText
after the button is disabled. Useful if you want to get a li'l visual cue (such as changing the buttons text to 'Processing..').
The main work happens in the AddAttributesToRender
event:
Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
Dim strOnClick As String
If IsNothing(MyBase.Page) Then
MyBase.Page.VerifyRenderingInServerForm(Me)
End If
writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit")
writer.AddAttribute(HtmlTextWriterAttribute.Name, MyBase.UniqueID)
writer.AddAttribute(HtmlTextWriterAttribute.Value, Me.Text)
If Not IsNothing(MyBase.Page) And Me.CausesValidation_
And MyBase.Page.Validators.Count > 0 Then
If Me.DisableAfterClick Then
strOnClick = Me.GetClickOnceClientValidateEvent()
Else
strOnClick = Me.GetClientValidateEvent()
End If
If MyBase.Attributes.Count > 0 And Not _
IsNothing(MyBase.Attributes("onclick")) Then
strOnClick = String.Concat(MyBase.Attributes("onclick"), strOnClick)
MyBase.Attributes.Remove("onclick")
End If
writer.AddAttribute("language", "javascript")
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, strOnClick)
ElseIf DisableAfterClick = True Then
strOnClick = Me.GetOnceClickJavascript()
If MyBase.Attributes.Count > 0 And Not _
IsNothing(MyBase.Attributes("onclick")) Then
strOnClick = String.Concat(MyBase.Attributes("onclick"), strOnClick)
MyBase.Attributes.Remove("onclick")
End If
writer.AddAttribute("language", "javascript")
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, strOnClick)
End If
MyBase.AddAttributesToRender(writer)
End Sub
This creates the button programmatically as well as determines if the button causes validation and there are validators on the page or if the button just simply needs to be disabled. You'll notice it calls three distinct different properties. These are what makes the magic happen. They are as follows:
Friend ReadOnly Property GetClientValidateEvent() As String
Get
Return "if (typeof(Page_ClientValidate) _
== 'function') Page_ClientValidate(); "
End Get
End Property
Friend ReadOnly Property GetClickOnceClientValidateEvent() As String
Get
Return "if (typeof(Page_ClientValidate) == 'function') _
{ if(Page_ClientValidate()) { " + _
GetOnceClickJavascript + " }} else { " + _
GetOnceClickJavascript + " }"
End Get
End Property
Friend ReadOnly Property GetOnceClickJavascript() As String
Get
Return "document.getElementsByName('" + _
Me.OnceClickBtnName + "').item(0).setAttribute('name'," + _
"this.getAttribute('name')); this.disabled = true; " + _
IIf(DisabledText = String.Empty, String.Empty, _
"this.value = '" + DisabledText + "';") + _
"this.form.submit();"
End Get
End Property
The property, GetClientValidateEvent()
, returns the same JavaScript that the framework normally appends if your button control causes validation and there are validators on the page. This is to handle the scenario where you have the button on a page, set to not be disabled after click, and you have validators.
The property, GetClickOnceClientValidateEvent()
, returns a slightly modified version of the above script. This handles the scenario when there are validators on the page and you want the button to be disabled after click. But you want to make sure it only gets disabled if the Page_ClientValidate()
returns true or in the case of non IE browsers it'll just disable the button and submit.
The property, GetOnceClickJavascript()
, returns the base set of JavaScript that disables handles disabling the button. It also makes sure the button gets sent over in the post so that the button click will be raised. It does this by exchanging the name attribute of a hidden field with the name of the button. The framework just needs to see the name/ID of the control sent over to raise its click event. The hidden field gets registered in the control's OnInit(EventArgs)
event.
Protected Overrides Sub OnInit(ByVal e As EventArgs)
If Me.DisableAfterClick And Not Me.IsHiddenFieldRegistered Then
MyBase.Page.RegisterHiddenField(Me.OnceClickBtnName, "")
End If
MyBase.OnInit(e)
End Sub
Private Function IsHiddenFieldRegistered() As Boolean
For Each ctl As Control In MyBase.Page.Controls
If TypeOf ctl Is HtmlControls.HtmlInputHidden Then
If ctl.ID = Me.OnceClickBtnName Then
Return True
End If
End If
Next
Return False
End Function
The default constant value of OnceClickBtnValue
is __onceClickBtn
. The IsHiddenFieldRegistered
function just makes sure that the field is registered only once. This allows us to possibly have more than one button on the form that disables after click.
You may be wondering why I didn't just inherit from the System.Web.UI.WebControls.Button
class. Well, the reason was the AddAttributesToRender
event. I could have just hijacked that event and added the same code I have here and not have had to go through the entire process of creating the properties, functions, etc. The problem was I still needed to call MyBase.AddAttributesToRender
which would then finally call the AddAttributesToRender
in the System.Web.UI.WebControls.WebControl
class. If I inherited from the Button
class, I would end up with two onclick
s and no control over the JavaScript outputted in the event of validation. This gives me complete control.
To use this in your own projects, simply add the clickoncebutton assembly to your toolbox and drag and drop it onto your webform and you're set! Enjoy!
Note: Tested on IE 6.0 and NS 7.0 with version 1.1 of the .NET framework.