views:

827

answers:

3

Hey guys,

the following problem arises. I created an entry template for data which is repeated several times. Now I want to make sure at least one of these items is filled in.

the way I do this is use the Count property from a generic list List to see how many are are used(there's some logic in my control that allows me to just pick out the entries that are actually used).

Is there any way I can add something to the validationsummary of the page to notify the user that at least one itme needs to be filled in. I want to do the count-check in the code behind ...

if (EnteredClasses.Count > 0)
{
    //do stuff here
}
else
{
    //show validation error
}

any ideas ?

thx, J.

A: 

Use a CustomValidator. When using these you can write any logic you like.

RichardOD
+1  A: 

I would use the CustomValidator control, inline with the ServerValidate function and also check for the Page.IsValid. The error message then for your CustomValidator control will show up in the validation summary if the ServerValidate function sets the args.IsValid to false.

Andrew

REA_ANDREW
I didn't know customvalidators didn't have the ControlToValidate requirement till now, thx :)
Jan W.
A: 

I would cheat and merge two methods. As per your question you seem to require the use of the validation summary so you need the basic asp validation. Then you need to use a custom validator.

The problem with the custom validator, is that you must attach it to a particular control and then implement the javascrip method

   function ClientValidate(source, agruments)
   {
      // Do your check here where source is the span for the validator and 
      // argument is an object with .value the value of the control to which 
      // the validator is attached and .isvalid that indicate if the validation 
      // checks out (that's what you set to true or false)
   }

The problem if that you want to validate multiple items not juste one. so I would implement this method and ignore arguments.value to then use jQuery to check your whole form and do whatever validation you need and then set isvalid.

Philippe Asselin