Last post Feb 05, 2011 12:40 AM by jkirkerx
Feb 03, 2011 01:04 PM|trickst3r|LINK
I'm trying to add validation support to DropDownList by creating a custom control inheriting from DropDownList.
To avoid naming conflicts my dropDownList implements INamingContainer. Unfortunately when I try using this control I get the following exception:
Unable to find control id 'MDD' referenced by the 'ControlToValidate' property of 'MDD_Validator'
This is my control:
public class MyDropDown: DropDownList, INamingContainer
private const string ValidatorID = "Validator";
private RequiredFieldValidator _validator;
protected override ControlCollection CreateControlCollection()
return new ControlCollection(this);
protected override void CreateChildControls()
_validator = new RequiredFieldValidator
ID = ValidatorID,
ControlToValidate = ID,
InitialValue = String.Empty,
ErrorMessage = "*",
ForeColor = Color.Red
protected override void Render(HtmlTextWriter writer)
I found out that excecption occurs because for INamingContainer FindControl(NamingContainerId) returns null, therefore validator cannot find dropdown.
When I remove INamingContainer implementation everything works fine, but then ID given to the validator is not associated with that of DropDown.
I know I can manually set validator ID to have DropDown.ID + "_" as a prefix, but is there any way to accomplish this using INamingContainer ?
Feb 04, 2011 12:09 AM|jkirkerx|LINK
private const string ValidatorID = "ct100$_Validator";
all controls get renamed on render unless you control thenaming process, check the browser source for the correct id name
Feb 04, 2011 09:29 AM|trickst3r|LINK
Thanks for reply but the ValidatorID is not the issue here, and changing it as you suggested makes no effect.
The problem is that I want the validator to be inside a separated naming container, just as if it was inside a UserControl. If I created a User Control and put a DropDown and a Validator inside it, then (after rendering), their ids (ClientID's actually)
would be somethign like this:
UserControl1_DropDown and UserControl1_Validator (assuming that ID of the user control is UserControl1, and for DropDown is DropDown, etc.)
Feb 05, 2011 12:40 AM|jkirkerx|LINK
Yes your right
You should really drop the validator control because it is flakey, not reliable, and adopt the jquery 1.41 and paint the control pink if the data is not valid.