2016-11-18 107 views
0

我在這裏讀到一篇很好的文章:(顯示數據與DataList和Repeater控件(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單選按鈕列表與直放站

我試圖給文章作者回應,但我無法通過加我的賬戶Facebook,Twitter等工作,通過網站問我的問題,所以我雖然我會問這裏。

該示例非常全面且易於遵循,但我希望看到一個RadioButtonList(例如性別)x男性女性,顯示DB字段值。 這將是一個很大的幫助,以讚美文章內容

THX

+0

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

代碼和問題是...?請採取[旅遊](http://stackoverflow.com/tour)並閱讀[我如何問一個好問題](http://stackoverflow.com/help/how-to-ask)? – VDWWD

+0

我的問題是:你如何添加一個RadioButtonList到DataList和Repeater? – James

回答

0

首先,你需要的RadioButtonList添加到DataListRepeater(它們都運行相同的,所以我將只提供一個例子),並添加OnItemDataBound事件。背後

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