Search code examples
asp.netvb.netaverageradiobuttonlistselectedvalue

total value of all radiobuttonlist values


I have 20 radiobuttonlists on a page. Each has 4 options with values 1, 2, 3 and 4.

What I need to do is on submitting the form, get the total value of all the radiobuttonlists (eg 3+1+2+3+4...) divided by the total number that have actually been filled in (none of them are required fields so anything from 0 to 20 of them could have been filled in) - hence getting an average value.

Is there an easy / elegant way of doing this?


Solution

  • I would embed the RadioButtonLists in a Panel or an other Container-control. Then you can loop its control-collection to get all RadioButtonLists.

    Do you want to divide by the number of RBL's or by the number of selected RBL's?

    Example that divides by RBL-Count,hence counts non selected as zero, and rounds to next integer:

    aspx:

       <asp:Panel ID="OptionPanel" runat="server">
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="1" Value="1"></asp:ListItem>
                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                <asp:ListItem Text="3" Value="3"></asp:ListItem>
                <asp:ListItem Text="4" Value="4"></asp:ListItem>
            </asp:RadioButtonList>
            <!-- and so on ... -->
        </asp:Panel>
        <asp:Button ID="BtnCalculate" runat="server" Text="calculate average value" />
        <asp:Label ID="LblResult" runat="server" Text=""></asp:Label>
    

    and in codebehind:

        Protected Sub BtnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click
            Dim rblCount As Int32
            Dim total As Int32
            Dim avg As Int32
            For Each ctrl As UI.Control In Me.OptionPanel.Controls
                If TypeOf ctrl Is RadioButtonList Then
                    rblCount += 1
                    Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
                    If rbl.SelectedIndex <> -1 Then
                        Dim value As Int32 = Int32.Parse(rbl.SelectedValue)
                        total += value
                    End If
                End If
            Next
            If rblCount <> 0 Then
                avg = Convert.ToInt32(Math.Round(total / rblCount, MidpointRounding.AwayFromZero))
            End If
            Me.LblResult.Text = "Average: " & avg
        End Sub
    

    According to youre new informations that you need to count only the selected RadioButtonLists and ignore f.e. RadioButtonList14 completely, have a look:

    If rbl.SelectedIndex <> -1 AndAlso rbl.ID <> "RadioButtonList14" Then
       Dim value As Int32 = Int32.Parse(rbl.SelectedValue)
       total += value
       rblCount += 1 'count only the selected RadiobuttonLists'
    End If
    

    I have moved rblCount += 1 into the If rbl.SelectedIndex <> -1-Statement, besides i've added rbl.ID <> "RadioButtonList14" as additional restriction to ignore this RadioButtonList.