简体   繁体   English

带中继器的RadioButtonList

[英]RadioButtonList with Repeater

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 我在这里读了一篇很好的文章:(使用DataList和Repeater控件(C#)显示数据) https://www.asp.net/web-forms/overview/data-access/displaying-data-with-the-datalist和中继器/与数据列表和中继器控件一起显示数据

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. 我正在尝试回复文章作者,但无法在工作中通过Facebook,Twitter等添加我的帐户,以通过网站提出我的问题,因此尽管我会在这里提出问题。

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. 该示例非常详尽且易于理解,但我希望看到一个RadioButtonList(用Gender表示)x Male Female,显示DB字段值。 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. 首先,您需要将RadioButtonList添加到DataListRepeater (它们都工作相同,因此我仅提供一个示例),并添加OnItemDataBound事件。

 <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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM