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
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.