0
我在頁面上有20 radiobuttonlists
。每個選項都有4個選項,其值分別爲1,2,3和4.所有radiobuttonlist值的總值
我需要做的是提交表單,獲得所有radiobuttonlists
(例如3 + 1 + 2 + 3 + 4)的總值。 )除以實際填寫的總人數(其中沒有一個是必填字段,因此可以填寫0到20之間的任何內容) - 因此獲得平均值。
有沒有一種簡單/優雅的方式來做到這一點?
我在頁面上有20 radiobuttonlists
。每個選項都有4個選項,其值分別爲1,2,3和4.所有radiobuttonlist值的總值
我需要做的是提交表單,獲得所有radiobuttonlists
(例如3 + 1 + 2 + 3 + 4)的總值。 )除以實際填寫的總人數(其中沒有一個是必填字段,因此可以填寫0到20之間的任何內容) - 因此獲得平均值。
有沒有一種簡單/優雅的方式來做到這一點?
我會在面板或其他容器控件中嵌入RadioButtonLists。然後,您可以循環其控制集合以獲取所有RadioButtonLists。
你想除以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>
和在代碼隱藏:
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"
作爲附加的限制忽略此單選按鈕列表。
我喜歡這個解決方案......當獲得平均值時是否可以忽略某些列? – Tom
@Tom:當然,但是定義「某些」列!你看我是如何計算總價值的。相應地定製它應該很容易。順便說一句,花了10天來評論我的答案? ;-) –
按專欄,我的意思是radiobuttonlists。抱歉。我需要忽略任何未填寫的內容,例如radiobuttonlist14。 – Tom