如何检查多个RadioButtonLists的选定值?

时间:2014-06-04 20:56:23

标签: c# html asp.net

我的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++;
    }
}

任何正确方向的帮助或观点都会非常有用!

由于

2 个答案:

答案 0 :(得分:2)

之前我做过类似的事情(即操纵不同的验证器控件)。我就是这样做的:

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

您可以将RadioButtonList列表定义为ASP.NET页面或用户控件中的私有字段

答案 1 :(得分:1)

如果您使用 runat =&#34;服务器&#34; 将所有RadioButtonLists放入DIV中,您可以轻松遍历该DIV内部的RadioButtonLists。这是一个在Button1单击时触发的简单示例,您可以在Button1单击事件的末尾放置一个调试点,以查看value1和value2的值。

ASP.NET代码隐藏:

<div id="myradiolist" runat="server">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Value="1" Text="1"></asp:ListItem>
        <asp:ListItem Value="2" Text="2"></asp:ListItem>
    </asp:RadioButtonList>

    <asp:RadioButtonList ID="RadioButtonList2" runat="server">
        <asp:ListItem Value="3" Text="3"></asp:ListItem>
        <asp:ListItem Value="4" Text="4"></asp:ListItem>
    </asp:RadioButtonList>

    <asp:RadioButtonList ID="RadioButtonList3" runat="server">
        <asp:ListItem Value="5" Text="5"></asp:ListItem>
        <asp:ListItem Value="6" Text="6"></asp:ListItem>
    </asp:RadioButtonList>

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

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

C#Code-Behind:

    protected void Button1_Click(object sender, EventArgs e)
    {
        int value1=0, value2=0;

        foreach (Control c in myradiolist.Controls)
        {
            if (c is RadioButtonList)
            {
                RadioButtonList rbl = (RadioButtonList)c;

                if(rbl.SelectedValue.Equals("1"))
                    value1++;

                if (rbl.SelectedValue.Equals("2"))
                    value2++; 
            }
        }           
    }