Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a custom control validatable?

I have a custom control based on a Panel. It's merely a simple panel with 3 DropDownLists. If I add a CompareValidator to my WebForm and set the ControlToValidate property to the ID of my custom control I get the following error:

Control '{0}' referenced by the ControlToValidate property of '{1}' cannot be validated.

I understand why, the CompareValidator cannot sensibly validate anything against the Panel. So what do I need to add to my custom control to make it possible for a validator to be able to validate it?

I have tried adding a Text property to my Panel which returns a combination of the Text properties of the 3 DropDownLists. No avail.

I have tried looking for certain interfaces (ITextControl) that validatable controls (TextBox) implement and add them to my custom control. No avail.

How can I make my custom control based on a Panel, validatable by the regular .net validators? (RequiredFieldValidator, CompareValidator, etc.)

like image 573
Bazzz Avatar asked May 09 '12 17:05

Bazzz


1 Answers

If you don't already have it, you need to add the ValidationProperty attribute to your class so it knows which property to use for validation:

[ValidationProperty("Text")]
[ToolboxData("<{0}:YourCustomControl runat=server></{0}:YourCustomControl>")]
public class YourCustomControl : WebControl
...
like image 120
James Johnson Avatar answered Oct 24 '22 06:10

James Johnson