Solved

asp Radiobuttonlist results not working when rbl's dynamically created

Posted on 2011-02-24
5
419 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:bchambers233
  • 4
5 Comments
 
LVL 14

Expert Comment

by:robasta
ID: 34968870
are you setting the ID when you create the buttons? post the code that creates the radio buttons
0
 

Author Comment

by:bchambers233
ID: 34968883
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

0
 

Author Comment

by:bchambers233
ID: 34968929
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

0
 

Accepted Solution

by:
bchambers233 earned 0 total points
ID: 34968947
I got it,

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

Open in new window

0
 

Author Closing Comment

by:bchambers233
ID: 35005090
I got the answer from another source.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now