I have a custom server control which wraps a RadEditor (basically a textarea). I am trying to add a CustomValidator to it dynamically, but I keep getting this error on initial pageload
Unable to find control id ‘RadEditor1’ referenced by the
‘ControlToValidate’ property of ”.
This is the code I’m using inside my server control to create the CustomValidator:
protected override void OnInit(EventArgs e)
{
var validator = new CustomValidator();
validator.CssClass = "validator-error";
validator.Display = ValidatorDisplay.Dynamic;
validator.ControlToValidate = this.ID;
validator.Text = "You've exceeded the maximum allowed length for this field";
validator.ClientValidationFunction = "checkLength";
this.Controls.Add(validator);
base.OnInit(e);
}
The problem is that
RadEditorimplementsINamingContainer, so ASP.NET winds up searching among your server control’s children for a control namedRadEditor1. Of course, it’s unsuccessful becauseRadEditor1doesn’t have a child control namedRadEditor1.The trick I use is to choose a special ID like
"."to mean the parent control itself:Then use
"."as theControlToValidate: