Populating an ASP.NET Repeater
Asked Answered
B

2

5

I have a repeater in my aspx:

<asp:Repeater ID="rptDummy" runat="server" OnItemDataBound="rptDummy_OnItemDataBound"
     Visible="true">
</asp:Repeater>

in the c# side of the web I wrote this function:

 protected void createRadioButtons(DataSet ds){
     List<System.Web.UI.WebControls.RadioButton> buttons = new List<System.Web.UI.WebControls.RadioButton>();
     foreach (DataTable dt in ds.Tables){
            foreach (DataRow r in dt.Rows){
               System.Web.UI.WebControls.RadioButton rb = new System.Web.UI.WebControls.RadioButton();
               rb.Text = r[1] + " " + r[2] + " " + r[3] + " " + r[4];
               rb.GroupName = (string)r[5];
               buttons.Add(rb);
            }
      }
      rptDummy.DataSource = buttons;
      rptDummy.DataBind();
 }

But when trying it, it shows nothing. What am I doing wrong?

Bolus answered 3/3, 2013 at 19:25 Comment(3)
post aspx code of repeater, code where fetching data, and where are you calling this method?Cryptoanalysis
You're binding a list of radio buttons to the repeater instead of the data. If you put the radio button in the repeaters itemTemplate and just bind the data you should be able to get itPylos
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.Brooklynese
S
12

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;
    }
}
Semicentennial answered 3/3, 2013 at 20:16 Comment(8)
Great! it works! Now, how can I "receive" all the selected ones?Bolus
Yes, you'd have to iterate through the Repeater items to find each RadioButtonList.Semicentennial
@Bolus Dorfsman, i updated the answer with some changes at almost every step, please revise your code. In the end i added an example of how to get the selected values.Semicentennial
@Mt.Schneiders, I've tried the iteration you proposed, but it does'n work. I get the string length is 0 :/Bolus
Try Request.Form[list.UniqueID] to get the posted value, are you sure you selected one radiobutton for that groupname on the page?Semicentennial
let us continue this discussion in chatBolus
Sorry, can't at the moment as i'm at work. Keep me posted of what you tried.Semicentennial
What if it's for something like this: #31254626Disciplinarian
C
1

You should put the RadioButton in repeater and bind it in the createRadioButtons event.

Cryptoanalysis answered 3/3, 2013 at 19:34 Comment(1)
Could you please be more detailed?Bolus

© 2022 - 2024 — McMap. All rights reserved.