【问题标题】:total value of all radiobuttonlist values所有单选按钮列表值的总值
【发布时间】:2011-02-11 14:59:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: asp.net vb.net average radiobuttonlist selectedvalue


    【解决方案1】:

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

    您要除以 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 并忽略 f.e. 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 &lt;&gt; -1-Statement 中,此外我还添加了rbl.ID &lt;&gt; "RadioButtonList14" 作为忽略此 RadioButtonList 的附加限制。

    【讨论】:

    • 我喜欢这个解决方案...获取平均值时是否可以忽略某些列?
    • @Tom:当然,但是定义“某些”列!你看我是如何计算总价值的。相应地对其进行自定义应该很容易。顺便说一句,评论我的答案花了 10 天时间? ;-)
    • 列,我的意思是单选按钮列表。对不起。我需要忽略任何未填写的内容,例如,加上 radiobuttonlist14。
    • @Tom:您需要完全忽略radiobuttonlist14,或者如果未选择任何内容,您需要将radiobuttonlist14的值计为0?
    • 我需要完全忽略 radiobuttonlist14 - 我需要忽略任何未选择任何列表的列表。因此,如果未填写 list3、list6 和 list9,则在创建平均值时不要使用它们。这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多