tags:

views:

57

answers:

1

Hi,

I have a checkbox, the business rule is it has to be selected by user manually (so that he is aware what he is doing, not check automatically by the program).

If the user didn't check it, we need to show an error msg. How should apply the validation by ASP.NET? RequiredFieldValidator or something?

Thanks,

+2  A: 

You should use a CustomValidator instead of a RequiredFieldValidator.

For this to work, you'll have to add some javascript on the client side, as well as some code on the server side to perform validation logic.

There is an example here on how to set it up on the client side.

As for the server side, you just have to provide a method in the CustomValidator...

<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="onServerValidation" ErrorMessage="Pls check the value.." />

... and add the corresponding method in your code-behind:

void onServerValidation(object source, ServerValidateEventArgs arguments)
{
    arguments.IsValid = CheckBox1.Checked ;
}
Julien Poulin