CustomValidator bug?

  • Thread starter Thread starter George Ter-Saakov
  • Start date Start date
G

George Ter-Saakov

Hi.

I am fighting the CustomValidator strange behavior for second day already.
I created some test project which is only to reproduce the problem.

1. I created UserControl which has an only a Custom Validator
2. in UserControl::Page_Load i set
CustomValidator1.IsValid = false;

3. I insert the User Control into the page and have a submit button there.


So when i run the project it shows me errors from CustomValidator (because
IsValid set to false)
But when i click submit button there is no error shown and Page.IsValid
returns true.


Am i missing something? Where should i set CustomValidator1.IsValid = false;
so the Page.IsValid would be set to false as well?


Thanks.
George.
 
George Ter-Saakov said:
Hi.

I am fighting the CustomValidator strange behavior for second day already.
I created some test project which is only to reproduce the problem.

1. I created UserControl which has an only a Custom Validator
2. in UserControl::Page_Load i set
CustomValidator1.IsValid = false;

3. I insert the User Control into the page and have a submit button there.


So when i run the project it shows me errors from CustomValidator (because
IsValid set to false)
But when i click submit button there is no error shown and Page.IsValid
returns true.


Am i missing something? Where should i set CustomValidator1.IsValid = false;
so the Page.IsValid would be set to false as well?


Thanks.
George.

To implement a CustomValidator you need to handle the ServerValidate event.


private void CustomValidator1_ServerValidate(object source,
System.Web.UI.WebControls.ServerValidateEventArgs args)
{
args.IsValid = false;
}


Hans Kesting
 
Got to the bottom of it.
It turned out that I must provide handler CustomValidator.ServerValidate and
set args.IsValid = false in it.

Otherwise the IsValid is reset to true.

Which is not very good design. I would say if ServerValidate event is not
handled then IsValid must stay as is.

Thanks.
George.
 
Back
Top