title: ControltoValidate Property of Required Field validator
body: <p>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</p>
  
  <pre><code>&lt;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"&gt;&lt;/uicontrol&gt;
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>System.Web.HttpException: Control 'Vendor_Name_Vendor_Part_Number' referenced by the ControlToValidate property of '' cannot be validated.
  </code></pre>
  
  <p>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?</p>
  
  <p>Here is my code which throws this exception.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I have edited to include the entire method...</p>
  
  <p>Here's how I call from my method <code>AddRequiredValidator(panel, control, ddl, tr);</code>
  where panel is Panel type and control is UIControl, ddl is the actual dropdown or textbox control  and tr stands for table row.</p>
  
  <p>Sry as I missed to include full code earlier.</p>