How to iterate through radiobuttonlist

I have about 5 radiobuttonlist on a webpage.  I just want to make sure that something is selected.  They all default to no selection.  Here is the code, but I cant seem to get to where I tell if that perticular radiobutton has a selection or not.

Dim C As Control
        Dim frm As Control
        frm = FindControl("form1")
        For Each C In frm.Controls
            If TypeOf C Is RadioButtonList Then
                Dim myID As RadioButtonList
                myID = C.ClientID.ToString
                If myID.selecteditem = Nothing Then
                    VerifyAnswers = False
                End If
            Else
            End If

        Next

This isnt getting me what I need
mgmhicksAsked:
Who is Participating?
 
jppintoCommented:

Partial Class Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        IterateControls(Me)
    End Sub
 
    Private Sub IterateControls(ByVal control As Control)
        For Each c As Control In control.Controls
            If TypeOf c Is RadioButton Then
                 If C.Selecteditem = Nothing Then
                    VerifyAnswers = False
                 End If
            End If
        Next
    End Sub
End Class

Open in new window

0
 
jppintoCommented:
       Dim C As Control
        Dim frm As Control
        frm = FindControl("form1")
        For Each C In frm.Controls
            If TypeOf C Is RadioButtonList Then
                If C.Selecteditem = Nothing Then
                    VerifyAnswers = False
                End If
            End If
      Next
0
 
mgmhicksAuthor Commented:
using vb and it comes back with c.selecteditem is not a member of system.web.ui.control
0
 
tetorvikCommented:
You need to iteratively loop through the page controls to find all RadioButtonList controls. Below is a sample code that retrieves the first selected radiobuttonlist control. The sample can be probably written in much better way as I'm more like C# guy, but you get the idea.


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (FindSelectedControl(Me) Is Nothing) Then
             //do something
        End If
    End Sub
 
    Private Function FindSelectedControl(ByVal objSearchControl As System.Web.UI.Control) As Object
 
        Dim objChildControl As System.Web.UI.Control
        Dim objControl As System.Web.UI.Control
        Dim rblControl As RadioButtonList
 
        If TypeOf objSearchControl Is RadioButtonList Then
            rblControl = CType(objSearchControl, RadioButtonList)
            If Not rblControl.SelectedItem Is Nothing Then Return objSearchControl
        End If
 
        If objSearchControl.Controls.Count = 0 Then Return Nothing
 
        For Each objChildControl In objSearchControl.Controls
            objControl = FindSelectedControl(objChildControl)
            If Not IsNothing(objControl) Then Return objControl
        Next
 
        Return Nothing
    End Function

Open in new window

0
 
mgmhicksAuthor Commented:
Here is the final solution.  Split points because I needed both to solve the issue.  Here is the final code the key was the myControl = CType(c, RadioButtonList).  thanks

Private Function iterateControls(ByVal control As Control) As Boolean
        Dim myControl As New RadioButtonList
        For Each c As Control In control.Controls
            If TypeOf c Is RadioButtonList Then
                myControl = CType(c, RadioButtonList)
                If myControl.SelectedItem Is Nothing Then
                    iterateControls = False
                    Exit Function
                End If
            End If
        Next
        iterateControls = True
    End Function

Also instead of iteratecontrols(me) I had to use the form name.  iteratecontrols("Form1")
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.