HowTo: Make your custom ASP.net server control validatable
2 min read
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").Control "YourCustomControl" referenced by the ControlToValidate property of "theAddedValidator" cannot be validated.
[ValidationProperty("Value")]
public class MyCustomServerControl : WebControl, INamingContainer
{
...
//the property that will be validated
public String Value
{
get
{
...
}
set
{
...
}
}
...
}