开发者

Creating custom ASP.NET validator

开发者 https://www.devze.com 2022-12-12 06:07 出处:网络
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

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 wh开发者_开发技巧ich 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?


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.


Have you tried using validation groups?

0

精彩评论

暂无评论...
验证码 换一张
取 消