Getting the selected value from a RadioButtonList nested in a rangevalidation control (c#)

Good Morning C#/JavaScript Wizards,
I have been pulling out my hair trying to access the selected value of a RadioButtonList nested inside a RangeValidator
Here is the process i am looking for:
1) We have a 'Year of Birth' dropdown on a custom web control (and two instances of this control can exist on the page)
2) A RangeValidator if fired it the person's age is under 40
3) A RadioButtonList appears in this RangeValidator with two choices: 'I do' or 'I do not'
4) A dynamically added a onclick to the RadioButtonList (which fires).
5) The Controls ID is passed to a javascript function to check if the second button has been clicked (where the problem is)

What i do get:
1) control name
2) item.lenght
But i cannot access any of the items (i have tried 7 different answers i have found)

C# Code Behind:
  ListItem liContinue= new ListItem();
        liContinue.Text="I understand that I am not in the typical age group that is considered to be high risk of developing these diseases; however I still wish to be screened.";
        liContinue.Value = "1";
        rblUnderAge.Items.Add(liContinue);
 
        ListItem liLeave = new ListItem();
        liLeave.Text = "I do not wish to be screened.";
        liLeave.Value = "0";
        rblUnderAge.Items.Add(liLeave);
        rblUnderAge.Attributes.Add("onclick", "javascript:AgeOptOut(this.id);");
 
Javascript:
Simplist example:
function AgeOptOut(elementId) 
{
    var radioButtons = document.getElementsByName(elementId).item[1];
   alert(radioButtons.checked);
 }
Returns 'invalid' in alert popup

Open in new window

jvancojrAsked:
Who is Participating?
 
sunithnairCommented:
Try this mate. You were passing the id of the element and were using getElementsByName which in this case is different from the id.
<html>
<head>
<script type="text/javascript">
function AgeOptOut(elementId) 
{
   var elementName = elementId.replace(/_/g, "$")
   alert(elementName);
   var radioButtons = document.getElementsByName(elementName)[1];
   alert(radioButtons.checked);
 }
</script>
</head>
<body>
 <span id="ctl00_InsidePageContent_ciSecondUser_rv40orYonger" style="color:Red;display:none;">
<div id="ctl00_InsidePageContent_ciSecondUser_Panel1">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="GeneralTD">
<table id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge" onclick="javascript:AgeOptOut(this.id);" border="0">
<tr>
<td>
<input id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_0" type="radio" name="ctl00$InsidePageContent$ciSecondUser$rblUnderAge" value="1" />
<label for="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_0">I understand that I am not in the typical age group that is considered to be high risk of developing these diseases; however I still wish to be screened.</label>
</td>
</tr>
<tr>
<td>
<input id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_1" type="radio" name="ctl00$InsidePageContent$ciSecondUser$rblUnderAge" value="0" /><label for="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_1">I do not wish to be screened.</label>
</td>
</table>
</td>
</tr>
<tr>
<td class="GeneralTD">
Our vascular ultrasound screenings are not recommended for people 40 and under because the risk of finding significant disease is less likely in persons under 40 years of age unless there is a strong family history of vascular disease, extensive risk factors or the recommendation of your family physician. If you feel that you are at risk because of these factors, we will be happy to schedule your appointment.
<span id="ctl00_InsidePageContent_ciSecondUser_rfvUnderAge" style="color:Red;visibility:hidden;">
<img id="ctl00_InsidePageContent_ciSecondUser_Image15" src="images/Forms/required.gif" style="border-width:0px;" />
</span>
</td>
</tr>
</table>  
</div>
</span>
</body>
</html>

Open in new window

0
 
sunithnairCommented:
Did you try this?
function AgeOptOut(elementId) 
{
    var radioButtons = document.getElementsByName(elementId)[1];
   alert(radioButtons.checked);
 }

Open in new window

0
 
jvancojrAuthor Commented:
No, but it generated a javascript error when i ran it:
Line: 139
Char:4
Error: 'checked' is null or not an object
Code: 0

That is the line of the alert(radioButtons.checked);

btw...i misstyped the first error: alert displays 'undefined' and not 'invald'
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
sunithnairCommented:
If you can paste the html source (from the view source option of the browser) then i will be able to look at it and modify the code for you.
0
 
jvancojrAuthor Commented:
This is a 6 panel AJAX page so the html source is not small:
i have provided you with the key pieces

on the standard.js page: (included)
function AgeOptOut(elementId)
{
    var radioButtons = document.getElementsByName(elementId)[1];

   alert(radioButtons.checked);
 }
 <span id="ctl00_InsidePageContent_ciSecondUser_rv40orYonger" style="color:Red;display:none;">
<div id="ctl00_InsidePageContent_ciSecondUser_Panel1">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="GeneralTD">
<table id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge" onclick="javascript:AgeOptOut(this.id);" border="0">
<tr>
<td>
<input id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_0" type="radio" name="ctl00$InsidePageContent$ciSecondUser$rblUnderAge" value="1" />
<label for="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_0">I understand that I am not in the typical age group that is considered to be high risk of developing these diseases; however I still wish to be screened.</label>
</td>
</tr>
<tr>
<td>
<input id="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_1" type="radio" name="ctl00$InsidePageContent$ciSecondUser$rblUnderAge" value="0" /><label for="ctl00_InsidePageContent_ciSecondUser_rblUnderAge_1">I do not wish to be screened.</label>
</td>
</table>
</td>
</tr>
<tr>
<td class="GeneralTD">
Our vascular ultrasound screenings are not recommended for people 40 and under because the risk of finding significant disease is less likely in persons under 40 years of age unless there is a strong family history of vascular disease, extensive risk factors or the recommendation of your family physician. If you feel that you are at risk because of these factors, we will be happy to schedule your appointment.
<span id="ctl00_InsidePageContent_ciSecondUser_rfvUnderAge" style="color:Red;visibility:hidden;">
<img id="ctl00_InsidePageContent_ciSecondUser_Image15" src="images/Forms/required.gif" style="border-width:0px;" />
</span>
</td>
</tr>
</table>  
</div>
</span>

Open in new window

0
 
sunithnairCommented:
Are what do you pass as the value for elementId?
0
 
sunithnairCommented:
sorry i meant and what do you pass as the value for elementId?
0
 
sunithnairCommented:
Sorry i did not see the full code. I will get back to you soon
0
 
jvancojrAuthor Commented:
Thanx sunithnair...was exactly what i needed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.