Making a Check Box Required

B

Ben

Hi,

I have tried to associate a RequiredFieldValidator with a Check Box control,
but the Check Box control does not appear in the validator list of
ControlToValidate.

How can I make a Check Box control required?

TIA

Ben
 
B

Ben

Just wanted to post that I found a solution.

//
//-- In my code behind app, I Create an instance of CheckBox control
//
CheckBox chkBox = new ChkBox;
ChkBox.ID = "chkTest1";

//
//-- Create instance of CustomValidator control
//
CustomValidator oCV = new CustomValidator();
oCV.ClientValidationFunction = "ClientValidation"; // This refers to the
client-side javascript, see below.
oCV.ErrorMessage = "Value Required";
oCV.Display = ValidatorDisplay.Dynamic;

//
//-- I have added a custom attribute to the CustomValidator that stores the
"ID"
// of the checkbox control to validate. This is used in the client-side
jscript ("source" parameter).
//
oCV.Attributes.Add("checkboxcontrol", chkBox.ID);

//-- Here I add the CustomValidator control to the "TableCell" where the
CheckBox control exists
tbcCell2.Controls.Add(oCV);



//--Client-Side JavaScrip

<script language="javascript">
//-----
function ClientValidation(source, args)
{
var obj = null;
obj = FindControl(source.checkboxcontrol);
if (obj != null)
{
args.IsValid = obj.checked;
}
}
</script>
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top