所有ASP.NET數據綁定控件(直放站,ListView中,GridView的,等等)都可以爲他們的DataSource使用聲明式數據綁定。這不是很好的IMO文件,但你可以這樣做:
<asp:Repeater ID="rptCategory">
<ItemTemplate>
<asp:Repeater ID="rptSubcategory" DataSource='<%#Eval("Subcategories")%>'>
<ItemTemplate>
<asp:Repeater ID="rptItems" DataSource='<%#Eval("Items")%>'>
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
你需要綁定到DataSource
財產什麼的細節取決於你的初始數據(即rptCategory綁定到一個) 。
如果是DataSet
,則需要生成DataRelations
並使用相應的DataRelation
的名稱。
如果它是一個C#對象,那麼您將要使用集合屬性的名稱。
編輯:在這個特定的模式具體細節遵循....
對於您的模式,你將需要2個嵌套中繼器 - 1處理項目和1來處理的子類別(這通常會導致另一個嵌套中繼器其項目)。
使用數據集,你可以這樣做(注意,這是粗略的代碼,你需要填寫一些缺少的步驟):
var ds = ExecuteDataSet(
@"SELECT Id, Title FROM Cat;
SELECT Id, CatId, Title FROM SubCat;
SELECT Id, CatId, SubCatId, Title FROM Items;"
);
ds.DataRelations.Add("Cat_SubCat", ds.Tables["Cat"].Columns["Id"], ds.Tables["SubCat"].Columns["CatId"]);
ds.DataRelations.Add("Cat_Items", ds.Tables["Cat"].Columns["Id"], ds.Tables["Items"].Columns["CatId"]);
ds.DataRelations.Add("SubCat_Items", ds.Tables["SubCat"].Columns["Id"], ds.Tables["Items"].Columns["SubCatId"]);
this.rptCategory.DataSource = ds;
this.rptCategory.DataBind();
<asp:Repeater ID="rptCategory">
<ItemTemplate>
<h2>Category <%#Eval("Title")%></h2>
<asp:Repeater ID="rptCatItems" DataSource='<%#Eval("Cat_Items")%>'>
<ItemTemplate>
<h4>Item (Category) <%#Eval("Title")%></h4>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rptCatSubCat" DataSource='<%#Eval("Cat_SubCat")%>'>
<ItemTemplate>
<h3>SubCategory <%#Eval("Title")%></h3>
<asp:Repeater ID="rptSubCatItems" DataSource='<%#Eval("SubCat_Items")%>'>
<ItemTemplate>
<h4>Item (SubCategory) <%#Eval("Title")%></h4>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
這裏以前問:http://stackoverflow.com /問題/ 8454129 /高效的雙向結合嵌套中繼器-3的水平深。我應該回頭看看。對不起,投票結束重複。 – David
還有工作代碼在這個答案以及:http://stackoverflow.com/questions/5159736/asp-net-3-level-of-nested-repeater – David