I have 20 radiobuttonlists which are dynamically created - then declared when a form is submitted.
I also have some code which totals the number of answered questions and the total value of the answered questions. - this code used to work when the radiobuttonlists were hard coded into the page, but it now does not. - I am writing the number of questions answered and the total value of all answers to the page but they come back as 0.
Can anyone see why this might not work now that the radiobuttonlists are dynamically created.?
Code behind:
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
For i As Integer = 1 To 20
Dim TableRow As New TableRow()
Dim TableRowCell_1 As New TableCell()
TableRow.Cells.Add(TableRowCell_1)
holidayQuestionnaireTable.Rows.Add(TableRow)
Dim question As New RadioButtonList
question.ID = "question" & i
question.Items.Insert(0, new listitem("", "1"))
question.Items.Insert(1, new listitem("", "2"))
TableRowCell_1.Controls.Add(question)
Next
End Sub
...
Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
Dim question1 As RadioButtonList = DirectCast(Page.FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Page.FindControl("question2"), RadioButtonList)
Dim question3 ...
...
Dim question19 As RadioButtonList = DirectCast(Page.FindControl("question19"), RadioButtonList)
Dim question20 As RadioButtonList = DirectCast(Page.FindControl("question20"), RadioButtonList)
Dim rblCount As Double
Dim total As Double
Dim avg As Double
For Each ctrl As UI.Control In Me.myPanel.Controls
If TypeOf ctrl Is RadioButtonList Then
Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
If rbl.SelectedIndex > -1 And not rbl.ID = "question18" Then
Dim value As Double = Double.Parse(rbl.SelectedValue)
total += value
rblCount += 1
End If
End If
Next
Response.Write(rblCount & " - " & total & " - " & (total / rblCount))
End Sub
Body:
<asp:Placeholder ID="myPanel" runat="server">
<asp:Table runat="server" CellPadding="0" CellSpacing="0" GridLines="None" HorizontalAlign="Center" CssClass="ratingtable" ID="holidayQuestionnaireTable" />
<asp:Button OnClick="btnSendFeedback_Click" runat="server" Text="Submit..." ID="submitbutton" />
</asp:Placeholder>
You have changed the content of your panel and added a Table instead of using the Panel to add the RadioButtonLists directly. FindControl will only look into the NamingContainer of the Panel and not of its child controls' NamingContainer. Searching through the control-collection of the Panel does also not work because the RBL's are inside of the Table that is inside of the Panel. Therefore you have to loop the TableRows to get the RBL's. Have a look:
For Each row As TableRow In Me.holidayQuestionnaireTable.Rows
For Each cell As TableCell In row.Cells
For Each ctrl As Control In cell.Controls
If TypeOf ctrl Is RadioButtonList Then
Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
If rbl.SelectedIndex <> -1 AndAlso rbl.ID <> "question18" Then
Dim value As Int32 = Int32.Parse(rbl.SelectedValue)
total += value
rblCount += 1 'count only the selected RadiobuttonLists'
End If
End If
Next
Next
Next
If you want to use the FindControl-approach, you have to use the NamingContainer of each radioButtonList and that is the TableRow. So this would also work, but is very static and error-prone:
Dim question1 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(0).FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(1).FindControl("question2"), RadioButtonList)
Dim question3 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(2).FindControl("question3"), RadioButtonList)