你可以嘗試這樣的事情......
你的業務對象。你可以使用這個作爲一種模式,你可以從你的「真正的」業務對象變換或使用你的業務對象直接
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