dynamically creating controls, validators, using autopost back

G

Guest

I query database,
QuestionID | QuestionName | QuestionType
1 | FirstName | TextBox
2 | LastName | TextBox

Loop
Create TextBox
Create RequiredFieldValidator

Here is my problem.
If user enters LastName, then FirstName is required.
I can get LastName textbox to autopostback, but how do I create a sub that
will capture the autopostback and then enable the requiredFieldValidator.
I have many textboxes in the database so I need something that is generic?
thanks
 
L

Lucas Tam

If user enters LastName, then FirstName is required.
I can get LastName textbox to autopostback, but how do I create a sub
that will capture the autopostback and then enable the
requiredFieldValidator. I have many textboxes in the database so I
need something that is generic?

Why don't you bind the requirefieldvalidator when you created it the
first time?

So your loop:

Create Control
Create Validator
Bind Validator to Control

On postback:
Check validators
Valid
Continue
Else
Display Current Page
 
G

Guest

I do bind when i create them.
Loop
Create textbox.id = 1
Create requiredValidator.id = "req"&1
requiredValidator.controlToValidate = 1
requiredValidator.enabled = false

I set enabled to false because the FirstName is not required UNLESS you you
enter text into the LastName field. LastName is not required at all. In my
example you can leave both first name and last name empty, but if you enter a
last name, then you must enter a first name.
So, I have lastname autopostback, (I can't figure out how to capture the
autopost back to enable the fistname validator).
Next,
In my example I use FirstName and LastName, but in my database I have
something like pub0, pub1, pub2, pub3, pub4
so I wanted something generic to capture the postback, then I can strip the
string, get the number, then enable the corresponding, requiredFieldValidator.
 
L

Lucas Tam

I set enabled to false because the FirstName is not required UNLESS
you you enter text into the LastName field. LastName is not required
at all. In my example you can leave both first name and last name
empty, but if you enter a last name, then you must enter a first name.

Maybe you could use a custom validator? Write some sort of custom
validation routine that could your multiple cases.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top