Create VB.code Equal To JavaScript Loop Function

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
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.
Ron MalmsteadInformation Services ManagerCommented:
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ron MalmsteadInformation Services ManagerCommented:
Code example posted.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

EnflowAuthor Commented:

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 ManagerCommented:
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.
EnflowAuthor Commented:
@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
Jesus RodriguezIT ManagerCommented:
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
EnflowAuthor Commented:
@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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.