无限级联递归显示树

来源:互联网 发布:win7还原软件 编辑:程序博客网 时间:2024/06/01 15:38

 用的控件

 

           TreeView tvw_Directory;

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using SMQShare;
using FrameWorkV3.BLL;
using FrameWorkV3.Part;
using FrameWorkV3.Model;
using System.Collections;
using Shopping.Model;
using Shopping.Bll;

 

 

namespace Shopping.Part
{
    public class CategoryDirectory : BasePart
    {
        TreeView tvw_Directory;

        protected override void InitializeSkin(Control skin)
        {
            tvw_Directory = skin.FindControl("tvw_Directory") as TreeView;
            if (tvw_Directory != null)
            {
                DataBind();
            }
        }

        private new void DataBind()
        {
            if (tvw_Directory != null)
            {
                TProgram_Category program_Category = new TProgram_Category();
                ReturnValue result = program_Category.getProgram_CategoryListByProgramId(Sys_Info.ProgramId);
                if (!result.Success)
                {
                    Common.ProcessError(result);
                    return;
                }
                EntityList Categories = result.ObjectList;
                BuildTreeFromDataTable(Categories);
                BuildTreeFromProduct();
            }
        }

        #region Bind Category Treeview
        private void BuildTreeFromDataTable(EntityList entityList)
        {
            EntityList list = entityList.Select("ParentProgramCategoryId", "null");
            foreach (Entity entity in list)
            {
                TProgram_Category category = entity as TProgram_Category;
                TreeNode node = new TreeNode(category.Name, category.ProgramCategoryId.ToString());
                node.NavigateUrl = "C_" + category.Name + "_" + category.ProgramCategoryId.ToString() + ".ashx";
                tvw_Directory.Nodes.Add(node);
                node.Expand();
                BuildSubNode(node.ChildNodes, entityList, category.ProgramCategoryId);
            }
       }

        private void BuildSubNode(TreeNodeCollection childNodes, EntityList entityList, int ParentCategoryId)
        {
            EntityList list = entityList.Select("ParentProgramCategoryId", ParentCategoryId.ToString());
            foreach (Entity entity in list)
            {
                TProgram_Category category = entity as TProgram_Category;
                TreeNode SubNode = new TreeNode(category.Name, category.ProgramCategoryId.ToString());
                SubNode.NavigateUrl = "C_" + category.Name + "_" + category.ProgramCategoryId.ToString() + ".ashx";
                childNodes.Add(SubNode);
                SubNode.Expand();
                BuildSubNode(SubNode.ChildNodes, entityList, category.ProgramCategoryId);
            }
        }
        #endregion

        #region Bind Product Treeview
        private void BuildTreeFromProduct()
        {
            TProgram_Product program_Product = new TProgram_Product();
            ReturnValue result = program_Product.getProgramProductsByProgramId(Sys_Info.ProgramId,CustomerInfo.CustomerId);
            if (!result.Success)
            {
                Common.ProcessError(result);
                return;
            }
            EntityList products = result.ObjectList;
            foreach (Entity entity in products)
            {
                program_Product = entity as TProgram_Product;
                BindTreeNodeForProduct(program_Product);
            }
        }

        private void ExpandNode(TreeNode node)
        {
            node.Expand();
            if (node.Parent != null)
            {
                ExpandNode(node.Parent);
            }
        }

        public void BindTreeNodeForProduct(TProgram_Product program_Product)
        {
            BindTreeNodeForProduct(this.tvw_Directory.Nodes, program_Product);
        }

        private void BindTreeNodeForProduct(TreeNodeCollection childNodes, TProgram_Product program_Product)
        {
            foreach (TreeNode node in childNodes)
            {
                if (node.Value == program_Product.ProgramCategoryId.ToString())
                {
                    TreeNode subnode = new TreeNode(program_Product.Name, program_Product.ProgramProductId.ToString());
                    subnode.NavigateUrl = "P_" + Common.FilterUrl(program_Product.PartNumber) + "_" + program_Product.ProgramCategoryProductId.ToString() + ".ashx";
                    node.ChildNodes.Add(subnode);
                    ExpandNode(node);
                    return;
                }
                else
                {
                    BindTreeNodeForProduct(node.ChildNodes, program_Product);
                }
            }
        }
        #endregion

    }
}

 

 

 

里面用到的方法----------------

 

 

 

 public EntityList Select(string selectField, string value)
        {
            EntityList entityList = new EntityList();
            foreach (Entity entity in this)
            {
                if (entity.GetType().GetProperty(selectField) != null)
                {
                    Type t = entity.GetType();
                    object o = t.GetProperty(selectField).GetValue(entity, null);
                    if (o == null)
                    {
                        if (value.ToString() == "null")
                        {
                            entityList.Add(entity);
                        }
                    }
                    else
                    {
                        if (o.ToString().ToLower() == value.ToLower())
                        {
                            entityList.Add(entity);
                        }
                    }
                }
            }
            return entityList;
        }

 

 

效果图如下

效果图

原创粉丝点击