所有单选按钮列表值的总和

发布于 2024-10-17 03:43:02 字数 235 浏览 10 评论 0原文

我在一个页面上有 20 个 radiobuttonlists。每个都有 4 个选项,值为 1、2、3 和 4。

我需要做的是提交表单,获取所有 radiobuttonlists 的总价值(例如 3+1+2+3+ 4...) 除以实际填写的总数(它们都不是必填字段,因此可以填写 0 到 20 之间的任何内容) - 从而获得平均值。

有没有一种简单/优雅的方法来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极度宠爱 2024-10-24 03:43:02

我会将 RadioButtonList 嵌入面板或其他容器控件中。然后你可以循环它的控制集合来获取所有RadioButtonList。

您想除以 RBL 的数量还是除以所选 RBL 的数量?

除以 RBL-Count 的示例,因此将未选择的计数为零,并四舍五入到下一个整数:

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>

并在代码隐藏中:

    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

根据您的新信息,您需要仅计算选定的 RadioButtonLists 并完全忽略 fe RadioButtonList14,看看:

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

我已将 rblCount += 1 移至 If rbl.SelectedIndex <> -1-声明,此外我还添加了 rbl.ID <> “RadioButtonList14” 作为忽略此 RadioButtonList 的附加限制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文