Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用自引用表创建TreeView嵌套结构_Asp.net - Fatal编程技术网

Asp.net 使用自引用表创建TreeView嵌套结构

Asp.net 使用自引用表创建TreeView嵌套结构,asp.net,Asp.net,我正在尝试使用自引用表字段创建一个TreeView嵌套结构。下面是一个简单的例子: Category 1 Product 1 Toy 1 Toy 2 Product 2 Toy 3 Toy 4 更多类别 数据库表有一个名为Category的表。ParentCategoryId指向作为父级的类别。因此,对于类别1,ParentCategoryId为null,因为它是父类。对于产品

我正在尝试使用自引用表字段创建一个TreeView嵌套结构。下面是一个简单的例子:

Category 1 
      Product 1 
          Toy 1 
          Toy 2 
      Product 2 
          Toy 3 
          Toy 4 
更多类别

数据库表有一个名为Category的表。ParentCategoryId指向作为父级的类别。因此,对于类别1,ParentCategoryId为null,因为它是父类。对于产品1,ParentCategoryId为类别1 id,对于玩具1,ParentCategoryId为产品1 id

我正在使用以下代码,但它没有成功生成TreeView ASP.NET

 public void BuildTree(List<Category> categories, TreeNode treeNode)
    {
        if (treeNode == null) return;

        TreeNode tnAdd = null;
        var categoryId = Guid.NewGuid();

        foreach (var category in categories)
        {
            if (category.IsBaseCategory)
            {
                tnAdd = new TreeNode();
                tnAdd.Text = category.Description;

                BuildTree((from c in categories
                           where c.ParentCategoryId == category.CategoryId
                           select c).ToList<Category>(), tnAdd);
            }
            else
            {
                tnAdd = new TreeNode();
                tnAdd.Text = category.Description;

                BuildTree((from c in categories
                           where c.ParentCategoryId == category.CategoryId
                           select c).ToList<Category>(), tnAdd);
            }

            if (tnAdd != null)
                treeNode.ChildNodes.Add(tnAdd);              
        }
    }
什么不成功? 如果是因为你什么也看不见。。。我看不出您在哪里将根节点添加到实际的树控件中。tnAdd需要添加到树控件的某个位置

如果这是因为你没有得到你所期望的一切:除非你已经在某个地方进行递归并且没有意识到它,否则我看不出上面的代码是如何达到玩具级的。在上面的代码中,你说base,然后是child,它包含两个级别。您的示例数据中有三个级别,因此在某些情况下,您需要考虑添加玩具。如果需要n个级别,可以递归地编写它。如果你只有三个等级,你可以重复你自己

---OP更新的更新

查看您的代码,您得到的是:

   for each category {
    if it is base
       add its children
    else if it is not base
       add its children

    add it to the tree
   }
这意味着每个项目都会在第一个foreach中命中并添加到树中,而不是按级别。 你想要的是

for each category{ 
    if it is base
       add base's children

       for each child [
          add child's children
          add child to the tree
       ]

       add base the tree
}
我现在没有时间去测试,对不起,应该快开始工作了

 public BuildTreeTop(List<Category> categories, TreeNode treeNode)
 {
    BuildTree((from c in categories
                           where c.IsBaseCategory == true
                           select c).ToList<Category>(), categories, tnAdd);
 }

 public void BuildTree(List<Category> currentLevel, List<Category> allCategories, TreeNode treeNode)
    {
        if (treeNode == null) return;

        TreeNode tnAdd = null;
        var categoryId = Guid.NewGuid();

        foreach (var category in currentLevel)
        {
            tnAdd = new TreeNode();
            tnAdd.Text = category.Description;

            BuildTree((from c in allCategories
                        where c.ParentCategoryId == category.CategoryId
                        select c).ToList<Category>(), allCategories, tnAdd);


            if (tnAdd != null)
                treeNode.ChildNodes.Add(tnAdd);              
        }
    }

谢谢你的回复!我已经更新了我的帖子!我认为在主应用程序中,它可以达到n级。再来一个帮助!我需要隐藏treeview控件的根节点。
 public BuildTreeTop(List<Category> categories, TreeNode treeNode)
 {
    BuildTree((from c in categories
                           where c.IsBaseCategory == true
                           select c).ToList<Category>(), categories, tnAdd);
 }

 public void BuildTree(List<Category> currentLevel, List<Category> allCategories, TreeNode treeNode)
    {
        if (treeNode == null) return;

        TreeNode tnAdd = null;
        var categoryId = Guid.NewGuid();

        foreach (var category in currentLevel)
        {
            tnAdd = new TreeNode();
            tnAdd.Text = category.Description;

            BuildTree((from c in allCategories
                        where c.ParentCategoryId == category.CategoryId
                        select c).ToList<Category>(), allCategories, tnAdd);


            if (tnAdd != null)
                treeNode.ChildNodes.Add(tnAdd);              
        }
    }