如何检查多个RadioButtonLists所选值? [英] How can I check the selected values of multiple RadioButtonLists?

查看:218
本文介绍了如何检查多个RadioButtonLists所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML 格式有多个 RadioButtonLists

I have a HTML form that has multiple 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>

我如何能够通过遍历每个单选按钮列表来选择每个价值?

这是我这么远,我在正确的轨道上,还是有更好的方式来做到这一点?

This is what I have got so far, am I on the right track or is there a better way to do this?

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++;

}

我也试过,但不能似乎得到它的正确

I have also tried this but cant seem to get it all correct

foreach(RadioButtonList rbl in ?????) 
{
    if (rbl.SelectedValue == "1")
    {
        value1++;
    }
    else if (rbl.SelectedValue == "2")
    {
        value2++;
    }
}

在正确的方向任何帮助或点将是非常有帮助!

Any help or point in the right direction would be very helpful!

感谢

推荐答案

我(即操纵不同验证程序控件)之前做过类似的事情。以下是我想做到这一点:

I've done similar things before (namely, manipulating different validator controls). Here's how I'd do it:

var radioButtons = new List<RadioButtonList>() 
{
    RadioButtonList1,
    RadioButtonList2,
    RadioButtonList3,
    RadioButtonList4,
};

foreach(RadioButtonList rbl in radioButtons) 
{
    if (rbl.SelectedValue == "1")
    {
        value1++;
    }
    else if (rbl.SelectedValue == "2")
    {
        value2++;
    }
}

您可以在ASP.NET页面或用户控件

You can define the list of RadioButtonList as a private field within your ASP.NET page or user control

这篇关于如何检查多个RadioButtonLists所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆