Link to home
Start Free TrialLog in
Avatar of bchambers233
bchambers233Flag for United Kingdom of Great Britain and Northern Ireland

asked on

asp Radiobuttonlist results not working when rbl's dynamically created

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.?

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

Open in new window

Avatar of robasta
robasta
Flag of Zimbabwe image

are you setting the ID when you create the buttons? post the code that creates the radio buttons
Avatar of bchambers233

ASKER

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

Open in new window

OK... just spotted a schoolboy error (totally unrelated to the problem I'm asking about but worth highlighting)

First set of code submitted...
Line25 should be
Response.Write(rblCount & " - " & total & " - " & (total / rblCount))

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bchambers233
bchambers233
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got the answer from another source.