Do you have a custom ASP.NET control that you want to allow your users to attach the standard ASP.NET validation controls to?  A quick web search turns up a lot of forum topics that seem to suggest it’s not possible.  But it is!  And it’s so easy.

Just add a ValidationPropertyAttribute to your custom control’s type declaration:

public class YourCustomControl : WebControl // or CompositeControl or whatever
	/// <summary>
	/// Gets/sets the textbox area of the control
	/// </summary>
	public string Text { get; set; }

	// more code here

Of course this only makes sense if your control has a field that can sensically be validated, such as a textbox area. Your property of course does not need to be named Text, but can be anything as long as you keep the property name and the value you pass to the ValidationPropertyAttribute’s constructor in sync.

One thought on “Make your ASP.NET custom controls work with the validation controls”
  1. That is great, thanks! It worked for me. Unfortunately, it doesn't work when you use the Ajax Control Toolkit's ValidatorCalloutExtender. I get a javascript error saying "Uncaught Sys.ArgumentNullException: Sys.ArgumentNullException: Value cannot be null.
    Parameter name: element" on line 3056 of ScriptResource.axd, which is in the Sys.UI.DomElement.addCssClass method.

Comments are closed.