あなたはこのようなことを試すことができます...
あなたのビジネスオブジェクト。これをモデルとして使用して、「実際の」ビジネス オブジェクトから変換したり、ビジネス オブジェクトを直接使用したりできます。
public class BusinessObject
{
public string Category { get; set; } //Your category
public int ID { get; set; } //Point of data entry and will be return on post
public string Name { get; set; } //A friendly name for your users
}
aspx マークアップ。CheckBoxList
実際のアイテムが含まれるカテゴリにリピーターを使用しています。これは、かなり拡張してスタイルを設定できます。
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="checkboxlist"
runat="server"
DataTextField="Name"
DataValueField="ID" />
</ItemTemplate>
</asp:Repeater>
ビジネス オブジェクトを取得する場所: コード ビハインドにメンバーが 1 つあります。このデータは、ビジネス レイヤー/層から取得する必要があります。
List<BusinessObject> MyBusinessObjects = new List<BusinessObject>();
そしてあなたのコードビハインド
protected void Page_Load(object sender, EventArgs e)
{
//Wire up the event to handle when items are bound to the repeater
this.myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound);
//Now actually bind the categories to the repeater
this.myRepeater.DataSource = GetCategories(MyBusinessObjects);
this.myRepeater.DataBind();
}
void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//Don't process items that are not item, or alternating item
if (!(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)) return;
//Grab a reference to the checkboxlist in our repeater
CheckBoxList checkboxlist = (CheckBoxList)e.Item.FindControl("checkboxlist");
//Now put our business objects of that category in it
checkboxlist.DataSource = GetItemsFromCategory(MyBusinessObjects, (string)e.Item.DataItem);
checkboxlist.DataBind();
}
//Helper to grab categories.
private IEnumerable<string> GetCategories(IEnumerable<BusinessObject> items)
{
return (from i in items
select i.Category).Distinct();
}
//Helper to grab the items in categories.
private IEnumerable<BusinessObject> GetItemsFromCategory(IEnumerable<BusinessObject> items, string category)
{
return (from i in items
where i.Category == category
select i);
}