Nested Repeaters in ASP.NET

前端 未结 4 1336
攒了一身酷
攒了一身酷 2020-12-01 05:13

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

4条回答
  •  醉话见心
    2020-12-01 05:46

    It's always cleaner to deal with the datasource than messing about with ItemDataBound, but this is even more the case when nesting Repeaters:

    
      
        
          
            
              <%#SomeExtractingMethodLikeEval()%>
            
          
        
      
    
    

    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 GetNames(Family fam)
    {
      foreach(Person p in fam.Members)
        yield return p.FirstName + " " + p.Surname;
    }
    protected IEnumerable 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
    

提交回复
热议问题