Link to home
Create AccountLog in
Avatar of evilmike
evilmikeFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Loop through all RadioButtonList Controls and count the totals

Hi
I have an asp.net page (vb) that asks the user 20 questions.  The questions are split over 2 sections (10 questions in each section), and each section is contained in its own Panel (both panels are visible).  Each Question (ID =  Question1, Question2, Question3, etc...) uses the RadioButtonList control and the user can answer the question in 1 of 4 ways (Poor[1], Acceptable[2], Good[3] or Excellent[4]).

Once the user has answered all questions, a submit button is pressed and the answers are emailed to me.  This all works fine.

I would like to create an Analysis section (at the end of the email that I recieve) which counts the number of Poor, Acceptable, Good and Excellent answers for each indivdual section and returns the totals for each section to me.  For this I need to go through each control and store the given answer, but I don't know how to loop through each control and do this.

I don't want to list each control indivdually eg:


      Dim chosenAnswer as integer = Question1.SelectedItem.Value

      If chosenAnswer = 1 Then
            Section1PoorCount += 1
      ElseIf chosenAnswer = 2 Then
            Section1AcceptableCount += 1
      ElseIf chosenAnswer = 3 Then
            Section1GoodCount += 1
      ElseIf chosenAnswer = 4 Then
            Section1ExcellentCount += 1
      End If

      (and so on for all questions!)


because I might have to expand the number of questions and the code would get too long.


How can I create functionality that loops through all RadioButtonList controls in one section, and counts the totals for each selected entry?
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of evilmike

ASKER

Thanks Tim, that works great!

As I mentioned in the Question, there is a possiblity that I will need to add more questions, and some of these questions may not be Radiobuttonlist controls.  Can the above code be improved to check that the control is a radiobutton before doing the rest?

i.e.

Dim intQuestion As Integer
For intQuestion = 1 To 20
    Dim Question As RadioButtonList = Ctype(FindControl("Question" & Ctype(intQuestion,String)),RadioButtonList)
    ## check Question is a radio button control, and if so, continue with the functionality, otherwise go to next control
        ....
evilmike,

You can check using:

If FindControl("Question" & CType(intQuestion,String)).GetType Is RadioButtonList Then


Tim
Tim,

Unfortunately that has produced the following error:

Compiler Error Message: BC30684: 'RadioButtonList' is a type and cannot be used as an expression.

Line 129:      If FindControl("Question" & CType(intQuestion,String)).GetType Is RadioButtonList Then
evilmike,

Missed a bit:

If FindControl("Question" & CType(intQuestion,String)).GetType Is GetType(RadioButtonList) Then

Should do the trick.

Tim.
Hi Tim

Sorry, but i'm getting this error now:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Line 127:     Dim Question As RadioButtonList = Ctype(FindControl("Question" & Ctype(intQuestion,String)),RadioButtonList)

Here's the section of code we're working on:

      Dim intQuestion As Integer
            
      For intQuestion = 1 To 18
            
      Dim Question As RadioButtonList = Ctype(FindControl("Question" & Ctype(intQuestion,String)),RadioButtonList)
            
      If FindControl("Question" & CType(intQuestion,String)).GetType Is GetType(RadioButtonList) Then

            Select Case Question.SelectedItem.Value
                  Case 1
                        If intQuestion <=7 Then
                              Section1PoorCount +=1
                        ElseIf intQuestion <=18 Then
                              Section2PoorCount +=1
                        End If
                  Case 2
                        If intQuestion <=7 Then
                              Section1AcceptableCount +=1
                        ElseIf intQuestion <=18 Then
                              Section2AcceptableCount +=1
                        End If
                  Case 3
                        If intQuestion <=7 Then
                              Section1GoodCount +=1
                        ElseIf intQuestion <=18 Then
                              Section2GoodCount +=1
                        End If
                  Case 4
                        If intQuestion <=7 Then
                              Section1ExcellentCount +=1
                        ElseIf intQuestion <=18 Then
                              Section2ExcellentCount +=1
                        End If
                  End Select
                  
            End If
      NEXT
evilmike,
>      Dim Question As RadioButtonList = Ctype(FindControl("Question" & 
> Ctype(intQuestion,String)),RadioButtonList)

>      If FindControl("Question" & CType(intQuestion,String)).GetType Is
> GetType(RadioButtonList) Then

Swop these two lines around, you need to check whether it is a radiobuttonlist first before trying to cast the control to a radiobuttonlist.
dioButtonList)
         
     If FindControl("Question" & CType(intQuestion,String)).GetType Is GetType(RadioButtonList) Then

        Dim Question As RadioButtonList = Ctype(FindControl("Question" & Ctype(intQuestion,String)),Ra Tim          Select Case Question.SelectedItem.Value
               Case 1
                    If intQuestion <=7 Then
                         Section1PoorCount +=1
                    ElseIf intQuestion <=18 Then
                         Section2PoorCount +=1
                    End If
               Case 2
                    If intQuestion <=7 Then
                         Section1AcceptableCount +=1
                    ElseIf intQuestion <=18 Then
                         Section2AcceptableCount +=1
                    End If
               Case 3
                    If intQuestion <=7 Then
                         Section1GoodCount +=1
                    ElseIf intQuestion <=18 Then
                         Section2GoodCount +=1
                    End If
               Case 4
                    If intQuestion <=7 Then
                         Section1ExcellentCount +=1
                    ElseIf intQuestion <=18 Then
                         Section2ExcellentCount +=1
                    End If
               End Select
               
          End If
     NEXT
Unfortunately that has produced another error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

If FindControl("Question" & CType(intQuestion,String)).GetType Is GetType(RadioButtonList) Then