在ASP.NET嵌套中继器 [英] Nested Repeaters in ASP.NET
问题描述
我有一个包含分层数据的类。我使用嵌套中继器要present这个数据在我的ASP.net web应用程序。我该怎么做呢?我只做过一级嵌套,我怎么说的五个层次?
I have a class that contains hierarchical data. I want to present this data in my ASP.net webapp using nested repeaters. How do I do this? I've only ever done one level of nesting, how do I do say five levels?
每个项目可以有零个或多个子项。我基本上只是在每个subleveling使用一些CSS的东西缩进。我不想使用TreeView控件,我要严格按照中继棒。
Each item can have zero or many sub items. I'm basically just indenting at each subleveling using some css stuff. I do not want to use the treeview control, I want to strictly stick with a repeater.
更新:结果
我的数据来自一个数据库。我有一个项目的DataTable的一些基本属性。
Update:
My data comes from a database. I have an item datatable with some basic properties.
Item
{
ID,
Name,
Description,
...
}
然后我有一个多对多表:
Then I have a many to many table with:
Parent
{
ParentID,
ChildID
}
我遍历的每个项目,并显示其子女;和它的孩子的孩子。我认为这将最好地嵌套的中继器来完成,但我可能是错的。
I'm iterating through each item and displaying its children; and its children's children. I assume this would best be accomplished with nested repeaters, but I could be wrong.
推荐答案
它总是清洁处理数据源比摆弄的ItemDataBound,但这更是这样嵌套中继器时:
It's always cleaner to deal with the datasource than messing about with ItemDataBound, but this is even more the case when nesting Repeaters:
<asp:Repeater DataSource="<%#ColOfCol%>" runat="server">
<ItemTemplate>
<tr>
<asp:Repeater DataSource="<%#Container.DataItem%>" runat="server">
<ItemTemplate>
<td><%#SomeExtractingMethodLikeEval()%></td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
内的数据源也可以是一个评估的财产,或返回枚举想一个方法的调用。要知道,它会与对象进行调用。我preFER写具体的版本,然后过载:
The inner datasource could also be an evaluated property, or a call to a method that returns the enumeration wanted. Just be aware that it will be called with an object. I prefer to write the specific version, and then overload:
protected IEnumerable<string> GetNames(Family fam)
{
foreach(Person p in fam.Members)
yield return p.FirstName + " " + p.Surname;
}
protected IEnumerable<string> GetNames(object famObj)
{
return GetNames((Family)famObj);
}
有一点要注意的是,如果你想获得当前对象的父中继比你有获得它:
One thing to be aware of is that if you want to get the current object in the parent repeater than you have to obtain it with:
((RepeaterItem)Container.Parent.Parent).DataItem
这篇关于在ASP.NET嵌套中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!