Reputation: 1487
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
Upvotes: 0
Views: 2970
Reputation: 4542
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++;
}
}
}
Upvotes: 1
Reputation: 1195
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
Upvotes: 2