|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionBack 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 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 The server control behaves exactly like the intrinsic button control with the exception of two new properties: When The main work happens in the 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, The property, The property, 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 You may be wondering why I didn't just inherit from the 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. | ||||||||||||||||||||