Create VB.code Equal To JavaScript Loop Function

Enflow used Ask the Experts™
Using Framework...
     ASP.NET Regular Web Form

Using Code Behind... & JavaScript & JQuery

My Issue...
I Need VB.code replacement for the following JavaScript Function --
(JavaScript Function may not be Syntactically Correct Since I Slightly modified its structure)
function myFunc(RBG) {
   var rbGrp = RBG; 
   var myVal;
   var radioButtons = document.getElementsByName(rbGrp);
      for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
	     myVal = radioButtons[x].id);
             return myVal;

Open in new window

This JavaScript Function is PASSED the GROUPNAME of a group of Radio buttons...

It loops thru all the individual radio buttons of that Group of buttons via that GROUPNAME...

and ihen it should return the one radio button that was selected...

I need this in Server Side code instead of client side JavaScript code...

I have already laid out 35 Groups of Radio buttons each with 2, 3 or 5 individual choices -- already built in my form so doing Radio Button List is now past as option

Any Experts have or can create this code equal to the JS code ??

thanks... CJ
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Bear in mind this is not a site to ask for someone to write your code for you.  
Try it yourself, post your code and any errors and we can point out improvements and what you are doing wrong.
Information Services Manager
    Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged

        Dim TheValue As String = RadioButtonList1.SelectedValue.ToString
        Dim TheItem As String = RadioButtonList1.SelectedItem.ToString
        Dim TheIndex As String = RadioButtonList1.SelectedIndex.ToString

    End Sub

Open in new window


   Private Function RadioButtonSelections(ByVal GroupName As String) As String
        Dim result As String = ""
        Dim stringresult As String = ""

        Select Case GroupName
            Case "RadioButtonList1"
                result = RadioButtonList1.SelectedValue.ToString()

            Case "RadioButtonList2"
                result = RadioButtonList2.SelectedValue.ToString()
            Case Else
        End Select

        Return result
    End Function

Open in new window

Ron MalmsteadInformation Services Manager

Code example posted.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!



Thank you for your code... Looks very nicely done...

I am not using RadioButtonList controls...
But This Should work for RadioButton controls with Each having an Unique Group Name for EACH Group of them... with a slight mod on control object specification ????

For example here is One Group of RadioButton controls...

<div class="divTableRow">
                        <div class="divTableCellTwo_12Pct">Gender:<strong>*</strong></div>
                        <div class="divTableCellTwo_38Pct">
                            <asp:RadioButton ID="GD_1_M" runat="server" GroupName="Gender_1" Text="Male" ToolTip="M" />
                            <asp:RadioButton ID="GD_1_F" runat="server" GroupName="Gender_1" Text="Female" ToolTip="F" />
                        <div class="divTableCellTwo_12Pct">Last4 SSN:<strong>*</strong></div>
                        <div class="divTableCellTwo_38Pct">
                            <asp:TextBox ID="Last4_SS_1" runat="server" Height="25px" Width="340px" Wrap="False" BackColor="#EAF4FF" Font-Size="Large" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" MaxLength="4" ToolTip="Enter Last 4 Numbers of Social Security Number"></asp:TextBox>

Open in new window

Ron MalmsteadInformation Services Manager

You can do them individually ... or loop through the child controls attributes within each div to check for the group.

GD_1_M.Checked the simplest way to see if it's checked individually.


@Ron... Ok... Thanks for the Help...

Seems like RadioButtonLists data are much easier to parse then the individual RBs even with unique Group Names... CJ
Sorry for the late response but you can do something like this
    Private Function RadioValue(ByVal GroupName As String) As String
        Dim RdValue As String = String.Empty
        For Each RdButton As Control In Page.Controls
            If TypeOf (RdButton) Is RadioButton Then
                If TryCast(RdButton, RadioButton).GroupName = GroupName Then
                    If TryCast(RdButton, RadioButton).Checked Then
                        RdValue = TryCast(RdButton, RadioButton).Text
                        Exit For
                    End If
                End If
            End If
        Return RdValue
    End Function

Open in new window

You can call the function and verify if is on the Group Name and if is on that group name then get what is checked. Also remember that this is for a simple control collection on the root of the page. if you have master page or any other control that has children controls on the page you need to loop through those recursively


@J. Rodriguez...

Yes... I appreciate you coming in right at the buzzer... :o) --

I will test this out... I was hoping someone would have this in their back pocket... ... Yes, I am trying to loop thru all the RButtons on one long Web Form by GroupName and pull the selected text value out for each RB Group... To insert values into variables for the Insert Info submit code into my SQL Server DB...

Thanks for the help... CJ

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial