views:

82

answers:

2

I have created a custom control and a custom validator (extending BaseValidator). On custom control I have set ValidationProperty("Values"). The problem is that validation doesn't work when postback is sent unless I execute Page.Validate(). And when I call Page.Validate() all validators are executed wich is not what I would expect on postback.

So the question is how do I create custom validator wich would be executed when control value changes and validates just that control?

A: 

Have you tried using validation groups?

Jim B
+1  A: 

That is not how validators work. Unless you are using a ValidationGroup setting, all the validators on your page will automatically fire. You do NOT have to explicitly call Page.Validate(). You DO need to wrap your code in a check like this, however:

if(Page.IsValid)
{
    //do something here
}

Unlike client-side validators, the server-side validation does NOT prevent the page from posting back and processing events as normal.

To create a control which only validates when the control value changes would require a bit of hackery, since the change event fires after the validators have been executed.

Bryan
Yes, this is actually sad. I just spoted that if I create two fields with two validators and one of the fields will have AutoPostBack property set to true, then when after client side validates first field and displays error message I enter text in second field all error messages dissapear :(
Sergej Andrejev