2

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.

Programmerzzz
  • 1,237
  • 21
  • 48

1 Answers1

0

I think the problem you're having is that you are trying to add a validator to a general Control object, but you can only add them to controls that take user input - namely TextBox, Dropdown, etc:

Not all Web server controls support validation controls. The standard controls that can be validated with a validation control are:

  • System.Web.UI.WebControls.DropDownList
  • System.Web.UI.WebControls.FileUpload
  • System.Web.UI.WebControls.ListBox
  • System.Web.UI.WebControls.RadioButtonList
  • System.Web.UI.WebControls.TextBox
  • System.Web.UI.HtmlControls.HtmlInputFile
  • System.Web.UI.HtmlControls.HtmlInputPassword
  • System.Web.UI.HtmlControls.HtmlInputText
  • System.Web.UI.HtmlControls.HtmlSelect
  • System.Web.UI.HtmlControls.HtmlTextArea

For an input control to be validated, the System.Web.UI.ValidationPropertyAttribute attribute must be applied to the control.

So you will need to cast your control parameter to the appropriate type to apply the validation to it.

The reason your error message is confusing is because you haven't given your validation control an Id so the message doesn't have a value to show you.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117