Custom validator and Checkboxes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a custom validator by extending CustomValidator in order to
validate three fields as follows:

If checkbox true then textboxA and textboxB must have data in.

The problem I am having is that whether the checkbox is ticked or not it
returns the value "on" to my validator code. This is all using the ideas of
DanielHac as at http://www.codeproject.com/aspnet/MultiDependValidator.asp &
he suggests extending the checkbox control as below. Despite doing this I
still ahvev the porblem.

Any ideas?

-------------------------------------------------------------


'Replacement for standard checkbox.
'This allows the checked property to be read by validation controls.
'Standard microsoft support for clienside validation of a checkbox is broken.
<ValidationProperty("ValidationValue")> _
Public Class ValidifiableCheckBox : Inherits
System.Web.UI.WebControls.CheckBox
Private mValidationTrueValue As String = Boolean.TrueString

Public Property ValidationTrueValue() As String
Get
Return mValidationTrueValue
End Get
Set(ByVal value As String)
mValidationTrueValue = value
End Set
End Property


Public ReadOnly Property ValidationValue() As String
Get
If Me.Checked Then
Return mValidationTrueValue
Else
Return System.String.Empty
End If
End Get
End Property

End Class
 
Why would you bother extending the checkbox? Just put the following code in
the CustomControl's ServerValidate eventhandler:


If mycheckbox.Checked Then
If mytextbox1.Text = "" Or mytextbox2.Text = "" Then
args.IsValid = False
Else
args.IsValid = True
End If
Else
args.IsValid = True
End If


If you have some reason for wanting to extend the CheckBox that I am
missing, let me know, but I see know reason to do that in your case. Good
Luck!
 
I should have said - that works server side but I just canlt get it to work
client side. Reserach suggested that the clinet side validation stuff
doesnlt work proerly for checkboxes (see URL in my last posting) and that one
needs to extend the checkbx as I have done. But I canlt get either the
extended or normal checkbox to work client side - when converted to string it
always returns the value "on" regardless of whether the box is ticked or not
 
Back
Top