Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-20
9
Medium Priority
?
488 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:jvancojr
  • 6
  • 3
9 Comments
 
LVL 16

Expert Comment

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

Open in new window

0
 

Author Comment

by:jvancojr
ID: 23694896
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
 
LVL 16

Expert Comment

by:sunithnair
ID: 23695133
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jvancojr
ID: 23695394
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
 
LVL 16

Expert Comment

by:sunithnair
ID: 23696058
Are what do you pass as the value for elementId?
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23696061
sorry i meant and what do you pass as the value for elementId?
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23696090
Sorry i did not see the full code. I will get back to you soon
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 23696176
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
 

Author Closing Comment

by:jvancojr
ID: 31549337
Thanx sunithnair...was exactly what i needed
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question