Using ServerSideValidationExtender on Custom Validation Controls

Topics: Web Client Software Factory
Nov 17, 2008 at 4:29 PM
I have generated a new Custom Validator control, overriding ControlPropertiesValid() to allow the validator to validate a new CheckBoxList control (which now has a validation property). This validator works perfectly when I use a button to submit the form.

However when I add the ServerSideValidationExtender the page stops behaving how I expected and cutting a long story short, the ServerSideValidationExtender does not function.

Is this the expected behaviour? Does it ONLY work with the standard .NET validators? I thought that since my validator inherits CustomValidator the ServerSideValidationExtender would still work.
Nov 20, 2008 at 9:21 AM
After further investigation I have discovered that I can use a standard CustomValidator control on a new CheckBoxList which I have given a validation property.

The ServerSideValidationExtender does not work even when it is acting on a standard CustomValidator control.

If the CustomValidator is validating a textbox then  it does work but not when it is Validating my new CheckBoxList.

I was under the impression that the ServerSideValidationExtender was interacting only with the Validation control and provided that validation control worked then the extender would work too. Any ideas why this may not be the case?
Developer
Nov 20, 2008 at 3:59 PM

Hi

 

The ServerSideValidationExtender control does not support cross field validation. It only allows you to perform an asynchronous callback to the server providing the value of the ValidationProperty that the control to validate has specified.

 

You should also consider the following:

·         The asynchronous callback is only performed if the value referenced by ValidationProperty changes because the ServerSideValidationExtender control uses a cache with the validated data to avoid performing unnecessary callbacks to the server.

·         In the handler of the ServerValidate event of the CustomValidator that is called asynchronically by the ServerSideValidationExtender, you should only use the value provided by the ServerValidateEventArgs arguments and you should not access to the Page’s controls. The full page is not being submitted (it’s using ASP.NET 2.0 Callbacks), so the state of the controls is not updated.

 

protected void MyCheckBoxListCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)

{

string rightValueToValidate = args.Value;

 

// You should not use the Page controls when the method is called asynchronically by the ServerSideValidationExtender.

string wrongValueToValidate = this.MyCheckBoxList.SelectedValue;

           

//...

}

 

What are you trying to validate in the CheckBoxList?

If you want to validate that the user selects something in the CheckBoxList, a possible workaround could be adding, for example, a hidden field related to your CheckBoxList control. By using Javascript you could update the value of this hidden field with the selected values in the CheckBoxList. Then, you can use the CustomValidator with the ServerSideValidationExtender to validate this field instead of the CheckBoxList.

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti

Nov 20, 2008 at 4:20 PM
It's a NewCheckBoxList the basic gist of which is below


    [ValidationPropertyAttribute("SelectedCount")]
    public class NewCheckBoxList : CheckBoxList
    {
        public int SelectedCount
        {
            get
            {
                int count = 0;
                foreach (ListItem item in this.Items)
                {
                    if (item.Selected)
                    {
                        count++;
                    }
                }
                return count;
            }
        }
    }

This enables me to use a standard CustomValidator which can use args.Value. I am only making sure that fewer than 3 boxes are checked!

protected void CustomValidator1_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        int i = Convert.ToInt32(args.Value);
        
        if (i <= 3)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

This is fine and works perfectly when i don't use the ValidationExtender. The extender isn't submitting behind the scenes when I change controls like it does when i have it running on a normal textbox and it even breaks my submit button after postback...

" The asynchronous callback is only performed if the value referenced by ValidationProperty changes because the ServerSideValidationExtender control uses a cache with the validated data to avoid performing unnecessary callbacks to the server."  - Would this cache not exist for my NewCheckBoxList? Would it be updated if i moved to a different control on the page?

I think I will try the hidden textbox idea because the ValidationExtender works perfectly on a textbox, I would just like to know the reason it doesn't seem to like my control mainly because it doesn't make sense.

Thanks for your help!


Developer
Nov 21, 2008 at 5:14 PM

Hi

 

The value in the SelectedCount property on your NewCheckBoxList class does not reflect the real amount of checkboxes checked when you are in the client side. This is because the value of the SelectedCount property is set on the server side (you have to perform a full post-back to the page).

 

Thus, when you use the ServerSideValidationExtender control, the first time that the page is requested the validation fails because the validation method is getting the previous value of the property. The next time the CustomValidator1_ServerValidate method is not even called because the value of the SelectedCount does not change in the client side.

 

The ServerSideValidationExtender control works only if the value to validate is modified in the client side (like the Text property of a TextBox).

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti