I have a user control named DynamicGenericControl. It actually generates the Textboxes, dropdowns etc from the properties it got and adds them to the Page as controls. For example
<uicontrol displayname="Manufacturer_Full Mfr Item #" datatype="DynamicGenericControl" colNumber="2" initRowNumber="1" isrequired="true" key="Vendor Name_Vendor Part Number" controltwotype="TextBox_TextBox" respondtoclick="true"></uicontrol>
In the above Example, my user control generates two textboxes as we have two columns specified as property. Now, I want to add arequiredfield validator for these two fields. I tried adding it and results in following exception.
System.Web.HttpException: Control 'Vendor_Name_Vendor_Part_Number' referenced by the ControlToValidate property of '' cannot be validated.
I just replaced, spaces with _. However I tried without that too. nothing worked. If we observe the exception, Controltovalidate Property of '' is what tricking me in. Was it my Validator was not added, or was it unable to find my ID?
Here is my code which throws this exception.
private static void AddRequiredValidator(Panel panel, UIControl uicontrol, Control control, TableRow tr)
{
TableCell validatorCell = new TableCell();
validatorCell.Width = Unit.Point(4);
if (uicontrol.IsRequired)
{
RequiredFieldValidator reqfield = new RequiredFieldValidator();
reqfield.ControlToValidate = control.ID;
reqfield.ErrorMessage = uicontrol.DisplayName + " is required.";
reqfield.Text = " !";
reqfield.ForeColor = Color.Red;
reqfield.Enabled = true;
reqfield.Style.Add("vertical-align", "top");
validatorCell.Controls.Add(reqfield);
}
tr.Controls.Add(validatorCell);
I have edited to include the entire method...
Here's how I call from my method AddRequiredValidator(panel, control, ddl, tr);
where panel is Panel type and control is UIControl, ddl is the actual dropdown or textbox control and tr stands for table row.
Sry as I missed to include full code earlier.