Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Validation + <asp:radiobutton>

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
I have 2 radio buttons (gender) and I simply want to ensure that one of these has been clicked.

Web Controls
<asp:RadioButton id="rdoGenderMale" GroupName="rdoGender" Text="male" CssClass="formtext" Runat="server"></asp:RadioButton>
<asp:RadioButton id="rdoGenderFemale" GroupName="rdoGender" Text="female" CssClass="formtext" Runat="server"></asp:RadioButton>

Custom Validator
<asp:CustomValidator runat="server" id="chkGenderValidator"
ErrorMessage="Please enter a gender value" ClientValidationFunction="chkGender" ControlToValidate="rdoGenderMale" Display="None"/>

Javascript
<script language="javascript">
function chkGender(source,args){
//default
args.IsValid = false;
with(document.frmBooking){
if((rdoGenderMale.checked) || (rdoGenderFemale.checked)){
args.IsValid = true;
}
}
return;
}
</script>

Error
System.Web.HttpException: Control 'rdoGenderMale' referenced by the ControlToValidate property of 'chkGenderValidator' cannot be validated.

I get this error when the page loads. If I change it both of these to <asp:radiobuttonlist> controls the error does not appear? Anyone any ideas?
System.Web.HttpException: Control 'rdoGenderMale' referenced by the ControlToValidate property of 'chkGender' cannot be validated.
 
Can't you use a RadioButtonList and the RequiredFieldValidator?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Only 1 of them is required though so I could not place a requiredvalidators on each control. I have inherited the code and to minimise changes I would like to be able to use the existing control which is <asp:radiobutton>, have you any idea why I am getting this error? The code is pretty straight forward.
 
A RadioButtonList is ONE control.. You only need to place ONE RequierdFieldValidator for the RBL you want.
 
Thanks for replies. Unfortunately in this instance I need to retain <asp:radiobutton> and I am wondering why I cannot perform custom validation on this control? Any help appreciated :)
 
A RadioButtonList will simply create a group of options. I can't think of a valid reason why you can't use that control especially as it is most suited to the job (the radio buttons that are generated by the RadioButtonList by default only allow one entry so that's already taken care for you - the required field validator would simply make sure that at least one was selected).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I have inherited this code and <asp:radiobutton> is the existing control. To avoid any cascading changes that may need to be done by changing the control I need to stick with the original. I cannot think of any reason as to why a custom validator cannot be used on this control. This is the avenue I need to follow. A custom validator on the <asp:radiobutton> control.
 
OK, if you go with that option it will be more complicated (one example of this is that the client id may not be what you think it is so any javascript may have to be registered server side) and will probably involve more work than simply changing the control to what it should have originally been.

I can only suggest what I think is the best method and control to use and it's up to you whether you choose to listen to this or not.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top