English 中文(简体)
所有单选按钮列表值的总值
原标题:total value of all radiobuttonlist values

我在一页上有20个单选按钮列表。每个都有4个选项,值分别为1、2、3和4。

我需要做的是在提交表单时,获得所有单选按钮列表(例如3+1+2+3+4…)的总值除以实际填写的总数(它们都不是必填字段,因此可以填写0到20个字段),从而获得平均值。

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

最佳回答

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

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

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

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>

并且在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

根据您的新信息,您只需要计算选定的单选按钮列表,而完全忽略f.e.单选按钮列表14,请查看:

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的附加限制。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签