【问题标题】:How can I check the selected values of multiple RadioButtonLists?如何检查多个 RadioButtonLists 的选定值?
【发布时间】: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。

标签: c# html asp.net


【解决方案1】:

我以前做过类似的事情(即操作不同的验证器控件)。这是我的做法:

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 页面或用户控件中的私有字段

【讨论】:

  • 我从没想过要列出清单! :P 工作得很好,谢谢
【解决方案2】:

如果您将所有 RadioButtonLists 放入带有 runat="server" 的 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# 代码隐藏:

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

【讨论】:

  • +1,除非您可能想再次编辑代码以将每个控件中的 Value 设置为 1 或 2。
  • @jp2code,但是 OP 在他的帖子中不需要这样做吗?还是我错过了什么? :)
  • 例如,在RadioButtonList4 中,您无法将值添加到value1value2,因为您的asp:ListItems仅包含 Value="7"Value="8"。 ...或者我错过了什么! :)
  • @jp2code,你是对的,这是真的。但是你应该问 OP 这个问题,因为我使用了他的确切 RadioLists,他可能错过了什么?
猜你喜欢
  • 2015-07-16
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
相关资源
最近更新 更多