你可以尝试这样的事情......
你的业务对象。你可以使用这个作为一种模式,你可以从你的“真正的”业务对象变换或使用你的业务对象直接
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);
}
来源
2011-06-14 22:12:56
Jay
感谢您的快速回复。我需要收集收集的数据并将其插入数据库。我现在使用的单个清单框可用于处理,但不允许对各个项目进行分组。我需要将成串的项目分组,并从数据库中为它们提供标题。 – techguy817 2011-06-14 22:08:51