Wednesday, May 25, 2005

ASP.Net Custom Validators

The other day I came across a tidbit of information that once stumbled upon, I realized I had known this long ago. I wish I had remembered it before I had to debug code for an hour, so I thought in the interest of prosperity, I would document this tidbit right now.

In our web solution, we make use of user controls rather extensively. My client had the need for one of my common controls to be validated, but only in one instance of its implementation. I thought, "Hey! This is the perfect time to use a custom validator." I dropped on my custom validator, set the ControlToValidate to my user control, added the OnServerValidate event and voila. Except for one problem...I got an error when I tried to open the page. Apparently, a user control is not allowed to be the target of a custom validator. So what to do? As has happened in the past, and as I am sure will happen again in the future, I thought of a work around :)

I decided to set the target control to a text box I had on the same form. Oops. This text box did not require an entry. My custom validator only fired when there was text in the textbox. It took me a couple of seconds to recognize my mistake, as I tried to figure out why my validator worked sporadically.

Long story short (too late???), if you want to use a custom validator, the validation event will only fire if the control you are validating has an entry.

13 comments:

Anonymous said...

That little detail has troubled me a number of times! This is also true for Regular Expression Validators and Range Validators. Validators will not fire if there is no data or selected value in the targeted control.

Valerie Vogt said...

Nice to know I am not the only one who has run into this.

RG said...

Ah, yes...yet another reason to use Peter Blum's Professional Validation and More. With VAM, you are much less likely to need a custom validator (because the provided validation options are much more sophisticated). Even if you have to use a custom validator, it will run even if a textbox is empty. Oh, and it has options for handling the problem of attaching a validator to a control in a different naming container.

The only downside is that the pricing has increased a bit from when it was first introduced. However, he's broken up the product into modules, and to meet this functionality probably only the essential validators module is required. If that is right, the price would be $50 per web server, or $500 for a developer redistribution license.

My preference is to minimize the use of the third party controls, but I have made an exception in this case because the controls are so useful.

Valerie Vogt said...

So do you use Peter Blum's validation tools against user controls?

RG said...

Well, it isn't possible to attach any validator (Microsoft or VAM) to a user control. Here's how Peter described it in one of his responses to a question regarding VAM:

Validator controls can be placed on a UserControl, or any container of other controls (Page, Panel, Table, DataGrid, etc).

The entity called a UserControl cannot be validated directly.
Validators work on data entry controls and the UserControl is not a data entry control. It is a container. For example, a UserControl does not have a property that returns the text value of an <input type='text'> tag.

One thing that is different between Microsoft's validators and mine is that my validators can be placed in different "naming containers" on the page which Microsoft's requires them to be the same. A naming container implements INamingContainer. It includes the Page, UserControl and rows of a DataGrid. So Microsoft would require that a validator appears in the same usercontrol as the textbox it evaluates. My controls let you place the validator anywhere, so long as you programmatically assign the ControlToEvaluate property to the
instance of the control that is evaluated.


So, it is true that you won't be able to do exactly what you want wanted. However, you will get to a solution with VAM much quicker than you will with the built-in validation controls.

Anonymous said...

It's been such a great day today! I thought I would do a little research on related stuff on the net and came across your site. I really appreciate your knowledge on the subject. Your site has some great resources. It's been a great help in creating more data entry companies info. Keep up the great work you are doing.

Anonymous said...

One of the ways around this problem is to leave the ControlToValidate property of the custom validator blank. This will force the validator to fire once every round trip.

This behaviour is documented here.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspplusvalid.asp

Anonymous said...

..."Ah, yes...yet another reason to use Peter Blum's Professional Validation and More. "...

Peter, is that you? LOL.

Helephant said...

If you're using ASP.NET 2.0 there's a property called ValidateEmptyText that you can set to true to make it validate even if the field is empty. Unfortunately there's nothing similar for ASP.NET 1.1.

Anonymous said...

Thanks for the tip. Been trying to get this work for over 2 hours. Will re-think my solution now.

Thanks

Santosh said...

Hi all,

Thanks a lot for this disscussion, I was wondering why validation was working. But now i got the solution "ValidateEmptyText".
:)

Anonymous said...

"One of the ways around this problem is to leave the ControlToValidate property of the custom validator blank. This will force the validator to fire once every round trip."

Thanks for this little gem. Just had a problem wit hmutliple validators on one page and only one of the validate events was firing...

Derryl Cocks said...

Thanks Val and Anonymous you saved me some coding time.