简体   繁体   English

如何递归填充ASP.NET Treeview?

[英]How to Populate an ASP.NET Treeview Recursively?

I have an ASP.NET treeview control that needs to be populated recursively from a series of objects. 我有一个ASP.NET树视图控件,该控件需要从一系列对象中递归地填充。 At the minimum, I need to hierarchically display the category name and description for each source and parent node. 至少,我需要按层次显示每个源节点和父节点的类别名称和描述。 To this end, I've written the following in my codebehind page: 为此,我在代码隐藏页面中编写了以下内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CATALooK.Automation;

namespace VBayUnMatched  
{
public partial class VBayCats : System.Web.UI.UserControl
{
    private int id;
    private CATALooK.Automation.SourceInfo source;
    private CATALooK.Automation.CategoryInfo parent;
    private string catID;
    private string catName;
    private string desc;
    private string rawData;
    private int advCatID;
    private DateTime lastUpdated;
    protected void Page_Load(object sender, EventArgs e)
    {
        CategoryController cc = new CategoryController();
        CategoryInfo ci = new CategoryInfo
        (id,source,parent,catID,catName,desc,rawData,advCatID,lastUpdated);
        //CATALooK.AdvCatInfoRemote[] acir = cc.getAdvancedCategories();
        TreeView trvUnMatchedCats = new TreeView();
        TreeNodeCollection tnColl = new TreeNodeCollection();
        TreeNode nodeSource = new TreeNode { Value = ToString(source) };
        TreeNode nodeParent = new TreeNode { Value = ToString(parent) };
        TreeNode nodeName = new TreeNode { Value = catName };
    }

    private void PopulateTreeview()
    {


    }

    private string ToString(SourceInfo source)
    {
        throw new NotImplementedException();
    }

    private string ToString(CategoryInfo parent)
    {
        throw new NotImplementedException();
    }

}

} }

How do I use recursion to assign parent to nodeParent, source to nodeSource and catName to nodeName? 如何使用递归将父级分配给nodeParent,将源分配给nodeSource,将catName分配给nodeName?

Thanks much for your help and guidance. 非常感谢您的帮助和指导。

The easiest (and most readable) way is to create a class that implements the IHierarchyData interface. 最简单(也是最易读)的方法是创建一个实现IHierarchyData接口的类。 This exposes a node of a hierarchical data structure, including the node object and some properties that describe characteristics of the node. 这将显示分层数据结构的节点,包括节点对象和一些描述节点特征的属性。 Objects that implement the IHierarchyData interface can be contained in IHierarchicalEnumerable collections, and are used by ASP.NET site navigation (like sitemaps) and data source controls. 实现IHierarchyData接口的对象可以包含在IHierarchicalEnumerable集合中,并由ASP.NET站点导航(如站点地图)和数据源控件使用。

a great sample of that is found here and here you see how to create IHierarchyData in a recursive manner 可以在 这里 找到一个很好的示例, 这里您将看到如何以递归方式创建IHierarchyData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM