Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

HowTo: Make your custom ASP.net server control validatable

2 min read

Content validation is a major issue of every application. It is an absolute "must have" to notify the user about invalid data he may have entered. Asp.net provides nice validators which you can drag onto your page and attach to a specific control by defining the ControlToValidate attribute of the validator.

So if you design your custom server control you would like to make it also validatable. Many approaches I've seen on the web is to directly integrate the validators in the custom server control and so to provide predefined validation functions. This can be useful in special cases s.t. the programmer/user of the server control doesn't have to bother about validation (i.e. create a custom control "AutoValidateTextBox").
A much cleaner approach however is to decouple validation from the implementation of the server control. This increases the flexibility for the user of the control in the way that he possibly can specify whatever validator he likes (if not generate and attach validators automatically). But if you just add your control to a page or user control and you add a validator, you'll get an error like:

Control "YourCustomControl" referenced by the ControlToValidate property of "theAddedValidator" cannot be validated.

To avoid this problem and to make your control validatable, you have to tell somehow which property exposed by your control is the one that should be validated. This can be done by specifying the following attribute on your server control's class:
[ValidationProperty("Value")]
public class MyCustomServerControl : WebControl, INamingContainer
{

...


//the property that will be validated
public String Value
{
get
{
...
}
set
{
...
}
}

...

}

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus