Custom validator and Checkboxes

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
 
N

Nathan Sokalski

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!
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top