Try this:
1 - Define the Repeater
:
<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound" >
<ItemTemplate>
<asp:RadioButtonList ID="rbl" runat="server" DataTextField="Item2" DataValueField="Item2" />
</ItemTemplate>
</asp:Repeater>
2 - Build the data structure and bind the repeater:
List<Tuple<string,string>> values = new List<Tuple<string,string>>();
foreach (DataTable dt in ds.Tables){
foreach (DataRow r in dt.Rows){
string text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
string groupName = (string)r[5];
values.Add(new Tuple<string,string>(groupName, text));
}
}
//Group the values per RadioButton GroupName
rptDummy.DataSource = values.GroupBy(x => x.Item1);
rptDummy.DataBind();
3 - Define the OnItemDataBound
event:
protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
IGrouping<string, Tuple<string, string>> group = (IGrouping<string, Tuple<string, string>>)e.Item.DataItem;
RadioButtonList list = (RadioButtonList)e.Item.FindControl("rbl");
list.DataSource = group;
list.DataBind();
}
}
You see, each IGrouping<string, Tuple<string, string>>
refers to a group of RadioButtons of a certain GroupName, they are also the items from the repeater. For each item we create a new RadioButtonList that represents the whole group of RadioButtons.
You can make it better by using a different DataStructure than a Tuple
, it is often unclear what Item1
and Item2
means.
UPDATE:
If you want to see the selected values:
protected void button_OnClick(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptDummy.Items)
{
RadioButtonList list = (RadioButtonList)item.FindControl("rbl");
string selectedValue = list.SelectedValue;
}
}