C# 如何检查多个RadioButtonList的选定值?

C# 如何检查多个RadioButtonList的选定值?,c#,html,asp.net,C#,Html,Asp.net,我有一个HTML表单,它有多个RadioButtonLists <div> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="1" Text="1"></asp:ListItem> <asp:ListItem Value="2" Text="2"></asp:ListItem>

我有一个HTML
表单
,它有多个
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>
我也试过,但似乎不能完全正确

foreach(RadioButtonList rbl in ?????) 
{
    if (rbl.SelectedValue == "1")
    {
        value1++;
    }
    else if (rbl.SelectedValue == "2")
    {
        value2++;
    }
}
任何帮助或指向正确的方向都会非常有帮助


谢谢

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

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++;
    }
}
var radioButtons=新列表()
{
RadioButtonList1,
RadioButtonList2,
RadioButtonList3,
RadioButtonList4,
};
foreach(单选按钮列表单选按钮中的rbl)
{
如果(rbl.SelectedValue==“1”)
{
value1++;
}
否则如果(rbl.SelectedValue==“2”)
{
value2++;
}
}

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

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

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++;
    }
}
var radioButtons=新列表()
{
RadioButtonList1,
RadioButtonList2,
RadioButtonList3,
RadioButtonList4,
};
foreach(单选按钮列表单选按钮中的rbl)
{
如果(rbl.SelectedValue==“1”)
{
value1++;
}
否则如果(rbl.SelectedValue==“2”)
{
value2++;
}
}

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

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

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++;
    }
}
var radioButtons=新列表()
{
RadioButtonList1,
RadioButtonList2,
RadioButtonList3,
RadioButtonList4,
};
foreach(单选按钮列表单选按钮中的rbl)
{
如果(rbl.SelectedValue==“1”)
{
value1++;
}
否则如果(rbl.SelectedValue==“2”)
{
value2++;
}
}

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

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

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++;
    }
}
var radioButtons=新列表()
{
RadioButtonList1,
RadioButtonList2,
RadioButtonList3,
RadioButtonList4,
};
foreach(单选按钮列表单选按钮中的rbl)
{
如果(rbl.SelectedValue==“1”)
{
value1++;
}
否则如果(rbl.SelectedValue==“2”)
{
value2++;
}
}

如果使用runat=“server”将所有RadioButtonList放入一个DIV中,您可以将
RadioButtonList
列表定义为ASP.NET页面或用户控件中的一个私有字段。您可以轻松地循环浏览该DIV中的RadioButtonList。下面是一个简单的示例,单击Button1时触发,您可以在Button1 click事件的末尾放置一个调试点,以查看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" />
    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++; 
            }
        }           
    }

如果使用runat=“server”将所有RadioButtonList放在一个DIV中,您可以轻松地仅循环该DIV中的RadioButtonList。下面是一个在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" />
    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++; 
            }
        }           
    }

如果使用runat=“server”将所有RadioButtonList放在一个DIV中,您可以轻松地仅循环该DIV中的RadioButtonList。下面是一个在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" />
    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++; 
            }
        }           
    }

如果使用runat=“server”将所有RadioButtonList放在一个DIV中,您可以轻松地仅循环该DIV中的RadioButtonList。下面是一个在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" />
    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++; 
            }
        }           
    }

你可以先把
value
做成一个数组,这样你就可以使用索引了……为什么不做一个数组,这样就可以访问它们呢?这将使您的
foreach
循环如下所示:
foreach(RadioButtonList rbl in/your array go here/)
。在HTML中,您的ASP项都没有ID值。这些都是必需的,这就是您的代码如何知道如何访问一个或另一个。@jp2code您是指单个列表项吗?实际上没有;代码不需要访问列表项本身就可以知道单选按钮组的值。清单项目在这方面是特别的;它们甚至不需要runat。你可以从创建一个数组开始,这样你就可以使用索引了……为什么不创建一个数组并以这种方式访问它们呢?这将使您的
foreach
循环如下所示:
foreach(RadioButtonList rbl in/your array go here/)
。在HTML中,您的ASP项都没有ID值。这些都是必需的,这就是您的代码如何知道如何访问一个或另一个。@jp2code您是指单个列表项吗?实际上没有;代码不需要访问列表项本身就可以知道单选按钮组的值。清单项目在这方面是特别的;它们甚至不需要runat。你可以从创建一个数组开始,这样你就可以使用索引了……为什么不创建一个数组并以这种方式访问它们呢?这将使您的
foreach
循环如下所示:
foreach(RadioButtonList rbl in/your array go here/)
。在HTML中,您的ASP项都没有ID值。这些都是必需的,这就是您的代码如何知道如何访问一个或另一个。@jp2code您是指单个列表项吗?实际上没有;代码不需要访问列表项本身就可以知道单选按钮组的值。清单项目在这方面是特别的;它们甚至不需要runat。你可以从创建一个数组开始,这样你就可以使用索引了……为什么不创建一个数组并以这种方式访问它们呢?这将使您的
foreach
循环如下所示