0

I read a very good article here: (Displaying Data with the DataList and Repeater Controls (C#)) https://www.asp.net/web-forms/overview/data-access/displaying-data-with-the-datalist-and-repeater/displaying-data-with-the-datalist-and-repeater-controls-cs

I was trying to respond to the article author but am unable to add my account through Facebook, Twitter, etc at work to ask my question through the site, so I though I would ask here.

The example is very thorough and easy to follow, but I would like to see a RadioButtonList (say with Gender) x Male Female, showing the DB field value. This would be a big help to compliment the article content

thx

James
  • 21
  • 1
  • 4
  • And your question is...? Please take [the tour](http://stackoverflow.com/tour) and read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask)? – VDWWD Nov 18 '16 at 20:22
  • My question is: How do you add a RadioButtonList to a DataList and Repeater? – James Dec 05 '16 at 15:59

1 Answers1

0

First you need to add the RadioButtonList to the DataList or Repeater (they both work the same so I will provide only one example) and add the OnItemDataBound event.

 <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
    <ItemTemplate>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
    </ItemTemplate>
</asp:DataList>

Code behind

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    //find the control with findcontrol and cast back to a radiobuttonlist
    RadioButtonList radioButtonList = e.Item.FindControl("RadioButtonList1") as RadioButtonList;

    //add some listitems, usually filled from list or database
    for (int i = 0; i < 5; i++)
    {
        radioButtonList.Items.Insert(i, new ListItem("ListItem " + i, i.ToString(), true));
    }

    //cast the dataitem to the datarowview
    DataRowView row = e.Item.DataItem as DataRowView;

    //set the correct radiobuttonvalue
    radioButtonList.SelectedValue = row["dbValue"].ToString();
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79