dynamic validators

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
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


-Brock
DevelopMentor
http://staff.develop.com/ballen
 
Back
Top