Recursive reading of List<Object>

前端 未结 5 1710
滥情空心
滥情空心 2020-12-17 07:18

I have this structure of List, to be specific it is a \"CategoryItem\" Object. Here\'s my declaration of \"CategoryItem\" Object.

         


        
      
      
      
5条回答
  •  有刺的猬
    2020-12-17 08:04

    You need to create a recursive Method that "renders" a category item. This methods needs to be aware of the level or current depth in order render to correct indenting:

    private void RenderCategory(HtmlTextWriter writer, CategoryItem item, int level)
    {
      writer.Write("
  • {1}
  • ", level * 5, HttpUtility.HtmlEncode(item.Name)); int nextLevel = ++level; foreach (CategoryItem child in item.SubCategories) { RenderCategory(writer, child, nextLevel); } }

提交回复
热议问题