The logic is wrong.
1. If the RadioButton is not checked, the validator should report no error.
It should return IsValid=true.
2. If the RadioButton is checked, test the value of the TextBox for text.
3. Do not set IsValid on the validator. The Validator does that to itself
automatically so your value will not be maintained. Instead, a
CustomValidator function requires that you set args.IsValid to true or
false.
Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object,
ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate
If RadioButton1.Checked Then
args.IsValid = TextBox1.Text.Trim().Length > 0
Else
args.IsValid = False ' not checked. Always valid
End If
End Sub
FYI: Having one control enable a validator is a common problem but always
requires custom coding. I wrote a replacement to Microsoft's validators that
doesn't require custom coding. The 25 validators of Professional Validation
And More (
http://www.peterblum.com/vam/home.aspx) have the Enabler property
where you define a rule that enables the validator based on the state of
other controls. It generates the correct client-side code and even supports
many more browsers than Microsoft's validators do in ASP.NET 1.x.
For example, my RequiredTextValidator with the Enabler set to monitor
radiobutton1 is checked looks like this:
<vam:RequiredTextValidator id=RTV1 runat=server ErrorMessage="Required"
ControlIDToEvaluate="TextBox1">
<EnablerContainer>
<vam:CheckStateCondition ControlIDToEvaluate="radiobutton1"
Checked="True" />
</EnablerContainer>
</vam:RequiredTextValidator>
--- Peter Blum
www.PeterBlum.com
Email:
PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
"Mattyw" <wi********@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,
I'm relatively new to Web Forms, I have been using Required Field
Validators and Regular Expression Validators on a Web Form I am
developing and everything works as expected using Visual Studio.NET
2003 and VB code behind.
I have a radio button and textbox, and basically i need the textbox to
be required based on the user selecting "Yes" from the radio button.
I've read through the newsgroups and found that a Custom Validator
could achieve this result, but am unsure how to implement it.
I have added a Custom Validator then a Required Field Validator to be
fired should the radiobutton be "Yes" and included it in the codebehind
page in the Custom Validator as below:
Private Sub CustomValidator1_ServerValidate(ByVal source As
System.Object, ByVal args As
System.Web.UI.WebControls.ServerValidateEventArgs) Handles
CustomValidator1.ServerValidate
if radiobutton.selectedvalue.text = "Yes"
then requiredfield.isvalid = true
else
requiredfield.isvalid = false
End if
End Sub
When I run the above the required field validator runs whatever is
checked by the radio button, so I'm obviously missing something or am I
thinking too logically???. I assumed that the above code would make the
required field validator "true" if "Yes" was selected and then display
the "error" and the validator would not fire based on "no" being
selected.
Any help would be grateful...I have a suspicion I'm going in the wrong
direction!!.
Cheers
Mattyw