dynamic validators

G

Guest

Is there a way to, based on the selection of another control, add a required
field validator to another field (code behind vb.net)?
I don't want the field to be required unless a checkbox or date field has
been entered.

Thanx.
 
B

Brock Allen

Sure, in Page_Load:

if (ConditionsMet)
{
RequiredFieldValidator val = new RequiredFieldValidator();
val.ControlToValidate = "idOfOtherControl";
val.Text = "* Required";
val.ErrorMessage = "You must provide a value for my other control";
somePlaceHolderInYourPage.Controls.Add(val);
}

So this assumes you have some <asp:placeHolder> or other control positioned
in the page where you want to add in your validation control.
 
G

Guest

thanx I'll try to convert this in VB.NET.

Brock Allen said:
Sure, in Page_Load:

if (ConditionsMet)
{
RequiredFieldValidator val = new RequiredFieldValidator();
val.ControlToValidate = "idOfOtherControl";
val.Text = "* Required";
val.ErrorMessage = "You must provide a value for my other control";
somePlaceHolderInYourPage.Controls.Add(val);
}

So this assumes you have some <asp:placeHolder> or other control positioned
in the page where you want to add in your validation control.
 
B

Brock Allen

If ConditionsMet Then
dim va as new RequiredFieldValidator() ';
val.ControlToValidate = "idOfOtherControl" ';
val.Text = "* Required" ';
val.ErrorMessage = "You must provide a value for my other control" ';
somePlaceHolderInYourPage.Controls.Add(val) ';
End If
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top