【发布时间】:2014-06-05 05:26:09
【问题描述】:
我有一个 HTML form 有多个 RadioButtonLists。
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
</asp:RadioButtonList>
</div>
<div>
<asp:RadioButtonList ID="RadioButtonList2" runat="server">
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
</asp:RadioButtonList>
</div>
<div>
<asp:RadioButtonList ID="RadioButtonList3" runat="server">
<asp:ListItem Value="5" Text="5"></asp:ListItem>
<asp:ListItem Value="6" Text="6"></asp:ListItem>
</asp:RadioButtonList>
</div>
<div>
<asp:RadioButtonList ID="RadioButtonList4" runat="server">
<asp:ListItem Value="7" Text="7"></asp:ListItem>
<asp:ListItem Value="8" Text="8"></asp:ListItem>
</asp:RadioButtonList>
</div>
如何遍历每个 RadioButtonList 以选择每个值?
这是我到目前为止所得到的,我是在正确的轨道上还是有更好的方法来做到这一点?
int value1= 0;
int value2= 0;
if (RadioButtonList1.SelectedValue == "1")
{
value1++;
}
else if (RadioButtonList1.SelectedValue == "2")
{
value2++;
}
if (RadioButtonList2.SelectedValue == "1")
{
value1++;
}
else if (RadioButtonList2.SelectedValue == "2")
{
value2++;
}
我也试过了,但似乎无法完全正确
foreach(RadioButtonList rbl in ?????)
{
if (rbl.SelectedValue == "1")
{
value1++;
}
else if (rbl.SelectedValue == "2")
{
value2++;
}
}
任何帮助或指向正确方向都会非常有帮助!
谢谢
【问题讨论】:
-
您可以先将
value设为一个数组,这样您就可以使用索引... -
为什么不创建Control Array 并以这种方式访问它们?这将使您的
foreach循环看起来像这样:foreach(RadioButtonList rbl in /Your array goes here/)。 -
在您的 HTML 中,您的 ASP 项都没有 ID 值。这些是必需的,这就是您的代码知道如何访问一个与另一个的方式。
-
@jp2code 您的意思是针对单个列表项?实际上没有;代码不需要访问列表项本身就可以知道单选按钮组的值是什么。列表项在这方面是特殊的;他们甚至不需要 runat。