你可以试试这样的...
您的业务对象。您可以将其用作可以从“真实”业务对象转换或直接使用业务对象的模型
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>
获取业务对象的地方:这里我的代码隐藏中只有一个成员。您应该从业务层/层中获取这些数据。
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);
}