Solved

Checkbox RequiredFieldValidator

Posted on 2004-03-24
6
807 Views
Last Modified: 2008-01-16
Does anybody know a simple way to ensure that a Checkbox is ticked, like using a RequiredFieldValidator for a Textbox ???????
0
Comment
Question by:leebee300
6 Comments
 
LVL 7

Expert Comment

by:culshaja
ID: 10667167
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 :-)
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 45 total points
ID: 10667261
if you can affort it you can set the Autopostback property of the checkbox to True
then on every click the page will be submitted and you will know if the checkbox is clicked or not
if you don't want to use this you have to create javascript function

for examle:

create
<intpu type="hidde" runat="server" id="iCheckBoxIsClicked" value="false">
<script language="JavaScript">
function RememberClickingOnCheckBox()
{
 iCheckBoxIsClicked.value = "true";
}
</script>

then when you submit the page you will have in you Request.FOrm collection an item called iCHeckBoxIsClicked with value true or false

you have to set onclick="RememberClickingOnCheckBox()" to the checkbox

HTH
B..G
0
 

Author Comment

by:leebee300
ID: 10667777
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Expert Comment

by:culshaja
ID: 10668113
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 :-)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10668155
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
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10668385
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 :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question