For years I've been living with the unsettling feeling that there's something mysteriously wrong with ASP.Net's CustomValidator control. Every time I made the courageous decision to tackle this elusive control and try to harness it for my use, something would go wrong. Something I couldn't quite put my finger on until now.
I've just discovered the terrible secret behind the CustomValidator control: it doesn't fire if the control to be validated has an empty value. Here it is, in black on white, from the MSDN Library:
Note If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.
WTF?! Who ever came up with *that* constraint? And why did I take so long to find this out? (answer: I should have been more patient and read the documentation through.)
The problem is that it's easy to imagine a scenario where checking whether a control is empty is an essential part of a more complex logic which warrants a CustomValidator. What then?
In my current scenario, I attached the CustomValidator to a TextBox to see whether it was empty or not, in conjuction with checking whether a DropDownList had a specific value. When I did that, the CustomValidator simply would not fire - not on the client nor on the server. Luckily for me, I was able to attach to the DropDownList instead of the TextBox and then the control would fire properly.
How did some of you overcome this limitation of ASP.Net's CustomValidator?