Building validators programatically

T

Trillium

I need to build controls and their associated validators dynamically,
depending on requirements from a database query. The database provides the
text of the questions, the type of control needed (textbox, radio buttons,
etc), and validation for the control (required, number, string etc).

I am going about this by checking the control requirements for each record,
declaring a new control of the type needed, then adding it to a placeholder
with .Controls.Add(input control). Then I check the validation for the
control, and declare and create the appropriate validator type(s), and add
them to the placeholder and also Validators with
Page.Validators.Add(validator control).

The controls build ok, and some of the validators work, but not all. I
notice that, upon submit, the validators collection only has the ones which
are hard-coded into the html. So maybe the Page.Validators.Add is not
enough, and maybe this is part of the problem.

I am new at .NET, so I may be going about this in the wrong way. What am I
missing?
 
T

Trillium

The first place I tried to add it was in Page_Load. Then I saw someone else
was creating the validator in Page_Init, and .Add-ing it to the Form instead
of the PlaceHolder. So I tried that, and the validator showed up in the
validators collection upon submit, but always with IsValid = True. So I'm
still not getting it.

Any thoughts on that?
 
T

Trillium

I've just re-read your reply, and I think there is a clue there. Do I have
this right?

I have been trying to create the validators at the same time as I create the
input controls: when the page is first called. Should I be waiting for when
the post-back occurs and THEN create the validators, possibly in Page_Load?
 
T

Trillium

Thank You. This was exactly what I needed to help figure out what the
problem was. I was calling Page.Validate() in Page_Load and I was not
carefully setting an ID for each validator. (Plus, I had Type = "Date"...
duh)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top