evilmike
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.Val ue
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?
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.Val
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
evilmike,
You can check using:
If FindControl("Question" & CType(intQuestion,String)) .GetType Is RadioButtonList Then
Tim
You can check using:
If FindControl("Question" & CType(intQuestion,String))
Tim
ASKER
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
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))
evilmike,
Missed a bit:
If FindControl("Question" & CType(intQuestion,String)) .GetType Is GetType(RadioButtonList) Then
Should do the trick.
Tim.
Missed a bit:
If FindControl("Question" & CType(intQuestion,String))
Should do the trick.
Tim.
ASKER
Hi Tim
Sorry, but i'm getting this error now:
Exception Details: System.InvalidCastExceptio n: Specified cast is not valid.
Line 127: Dim Question As RadioButtonList = Ctype(FindControl("Questio n" & Ctype(intQuestion,String)) ,RadioButt onList)
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("Questio n" & Ctype(intQuestion,String)) ,RadioButt onList)
If FindControl("Question" & CType(intQuestion,String)) .GetType Is GetType(RadioButtonList) Then
Select Case Question.SelectedItem.Valu e
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
Sorry, but i'm getting this error now:
Exception Details: System.InvalidCastExceptio
Line 127: Dim Question As RadioButtonList = Ctype(FindControl("Questio
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("Questio
If FindControl("Question" & CType(intQuestion,String))
Select Case Question.SelectedItem.Valu
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("Questio n" &
> Ctype(intQuestion,String)) ,RadioButt onList)
>
> 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("Questio n" & Ctype(intQuestion,String)) ,Ra Tim Select Case Question.SelectedItem.Valu e
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
> Dim Question As RadioButtonList = Ctype(FindControl("Questio
> Ctype(intQuestion,String))
>
> If FindControl("Question" & CType(intQuestion,String))
> 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))
Dim Question As RadioButtonList = Ctype(FindControl("Questio
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
ASKER
Unfortunately that has produced another error:
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
If FindControl("Question" & CType(intQuestion,String)) .GetType Is GetType(RadioButtonList) Then
Exception Details: System.NullReferenceExcept
If FindControl("Question" & CType(intQuestion,String))
ASKER
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("Questio
## check Question is a radio button control, and if so, continue with the functionality, otherwise go to next control
....