Link to home
Start Free TrialLog in
Avatar of leebee300
leebee300

asked on

Checkbox RequiredFieldValidator

Does anybody know a simple way to ensure that a Checkbox is ticked, like using a RequiredFieldValidator for a Textbox ???????
Avatar of culshaja
culshaja
Flag of United Kingdom of Great Britain and Northern Ireland image

The validation controls will only validate a control that has a ValidationPropertyAttribute set. This leaves you 2 options:

1. Inherit the checkbox control and set the property by using <ValidationPropertyAttribute("Checked")>

0r

2. Write your own custom validation code either as JavaScript or as code in the CodeBehind buttons onclick event.

Either way its not as simple as dragging a validation control.

Sorry,

James :-)
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leebee300
leebee300

ASKER

Culshaja,

please could you further explain point 1 if you dont mind, how would I go about this ????

thanks for anyhelp you could give me on this matter.
You define a class as follows:

Imports System.Web.UI.WebControls

<ValidationPropertyAttribute("Checked")> _
public class myCheckBox Inherits CheckBox

end class

You can add your own custom properties and methods to the control. A good example of how the Validation property is set can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuivalidationpropertyattributeclasstopic.asp

James :-)
Avatar of YZlat
why would you want to use RequiredFieldValidator for a checkbox?
checkbox, if checked has a value True, if unchecked has a value False. You don't need to user RequiredFieldValidator.
If you want to ensure that the value of a checkbox is always True, try a different validator
Using a different validator still means that the control needs the ValidationPropertyAttribute so that a validator will recognise it. You are right about using a different validator though. I would use a CustomValidator.

James :-)