0

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 which is not what I would expect on postback.

How do I create custom validator which would be executed when control value changes and validates just that control?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :(
0

Have you tried using validation groups?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.