Solved

asp Radiobuttonlist results not working when rbl's dynamically created

Posted on 2011-02-24
5
421 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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