I have a page with three radio buttons, depending on which button is selected I need to validate some controls (using required field validators). Other than using custom validators, is there any way to do this?
Thanks
I have a page with three radio buttons, depending on which button is selected I need to validate some controls (using required field validators). Other than using custom validators, is there any way to do this?
Thanks
Add an OnSelectedIndexChanged to the RadioButtonList (or CheckedChanged if they're individual Radio button controls)
In the code behind, .Enable
& .Disable
the specific required field validators.
ASP.NET Validators offer a client-side API that allows you to :
The syntax of the ValidatorEnable
function is :
ValidatorEnable(rfvMyValidator, boolState);
I tried this on a small example and it worked for me:
I have
-2textboxes: TextBox1 and TextBox2
-RequiredFieldValidator : RequiredFieldValidator1 with ControlToValidate="TextBox1"
-RadioButton : RequiredFieldValidator1
This code is generated by the RequiredFiledValidator:
<script type="text/javascript">
//<![CDATA[
var RequiredFieldValidator1 = document.all ? document.all["RequiredFieldValidator1"] : document.getElementById("RequiredFieldValidator1");
RequiredFieldValidator1.controltovalidate = "TextBox1";
RequiredFieldValidator1.errormessage = "RequiredFieldValidator";
RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
RequiredFieldValidator1.initialvalue = "";
//]]>
</script>
I want when the user click on the RadioButton1 to switch the validation to TextBox2.
This is how I did it:
protected void Page_Load(object sender, EventArgs e)
{
RadioButton1.Attributes.Add("onclick", "RequiredFieldValidator1.controltovalidate=\"TextBox2\"");
}