views:

395

answers:

2

I am currently re-writing a dynamic form based on database information.

This form contains numerous 'Item Specific' properties, each of which has a single control and label, for example Category has a label that says "Category" and a DropDownList that contains all the options.

The problem I now find is that each of these is added to the page by being created programatically based on some information from the database (Property name, options and tpye of property, such as text or dropdown), and each of these has at least one validator to ensure data entered in it is correct.

This provides a problem: How can I set the ControlToValidate on the validators (Also created programatically) when I have each component to be validated as an object, not a static thing on the aspx?

+2  A: 

Make sure your controls have ID's and set the Validator.ControlToValidate property accordingly.

George
Nice one, hadn't spotted that property!
Ed Woodcock
beat me to id :)
epitka
+2  A: 

Give a dynamic control an Id and then use that Id to set ControlToValidate. Something like this:

ddl = new DropDownList();
ddl.Id = "ddlWithMyID";

val = new SomeTypeOfValidator();
val.ControlToValidate = "ddlWithMyID";

Make sure that if you want to handle the events from dynamically created controls to re-create them and give them same Id. This can be done in Page_Init or even in Page_Load.

epitka