0

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>

How can I iterate through each RadioButtonList to select each value?

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!

Thanks

BRBT
  • 1,467
  • 8
  • 28
  • 48
  • You could start by making `value` an array, so you can use indexes... – Mr Lister Jun 04 '14 at 20:59
  • Why not make a [Control Array](http://stackoverflow.com/questions/5435293/how-do-i-make-a-control-array-in-c-sharp-2010-net) and access them that way? That would make your `foreach` loop look like this: `foreach(RadioButtonList rbl in /Your array goes here/)`. – Brandon Jun 04 '14 at 21:00
  • In your HTML, none of your ASP items have ID values. Those are required, and that is how your code knows how to access one versus another. –  Jun 04 '14 at 21:43
  • @jp2code You mean for the individual list items? No actually; the code doesn't need to access the list items themselves to know what the value of the radio button group is. List items are special in that regard; they don't even need runat. – Mr Lister Jun 05 '14 at 09:24

2 Answers2

2

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

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

Mario J Vargas
  • 1,185
  • 6
  • 12
1

If you put all RadioButtonLists inside a DIV with runat="server" you can easily loop through only RadioButtonLists inside of that DIV. Here is a simple example that is triggered on Button1 click, you can put a debug point at the end of Button1 click event to see the value of value1 and value2.

ASP.NET Code-Behind:

<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++; 
            }
        }           
    }
lucidgold
  • 4,432
  • 5
  • 31
  • 51
  • +1, except you might want to edit the code again to set the `Value` in each control to either a 1 or a 2. –  Jun 04 '14 at 21:35
  • @jp2code, but the OP does not require that in his post? Or am I missing something? :) – lucidgold Jun 04 '14 at 21:37
  • 1
    In `RadioButtonList4`, for example, you would not have a way to add a value to **value1** or **value2**, because your `asp:ListItems` contain only `Value="7"` and `Value="8"`. ...or I am missing something! :) –  Jun 04 '14 at 21:39
  • @jp2code, you are correct, thats true. But you should ask OP that question becuase I used his exact RadioLists, he might have missed something? – lucidgold Jun 04 '14 at 21:41