Linq写的Treeview递归生成结点

 public partial class AreaTree : System.Web.UI.UserControl
    {
        public delegate void ClickHandler(object sender, EventArgs e);//定义一个事件委托
        public event ClickHandler Click = null;
        private  BFLList areaList;
        private  IQueryable<PUB_C_AREA> Areas;
        public List<short?> AreaID
        {
            get
            {
                return AreaList();
            }
        }
        public string GetAreaName()
        {
            return TreeView1.SelectedNode.Text;
        }
        public void LoadTree()
        {
           
            InitTree(TreeView1.Nodes, 0);

        }
        protected void OnClick(EventArgs e)
        {
            if (Click != null)
                Click(this, e);
        }

 

         //递归生成树结点
        public void InitTree(TreeNodeCollection Nds, short parentid)
        {
            areaList = new BFLList();
            Areas = areaList.GetAreaAll();
            foreach (var area in Areas.Where(p => p.SubOfArea == parentid).Select(p => p))
            {
                TreeNode node = new TreeNode();
                node.Expanded = false;
                node.Text = area.NAME;
                node.Value = area.AREAID.ToString();
                // node.SelectAction = TreeNodeSelectAction.Expand;

                Nds.Add(node);
                InitTree(Nds[Nds.Count - 1].ChildNodes, area.AREAID);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadTree();
        }
       
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            this.OnClick(e);
        }
        //获取选取树节点及其所有所有子节点
        private List<short?> AreaList()
        {
            List<short?> ShtList = new List<short?>();
            areaList = new BFLList();
            Areas = areaList.GetAreaAll();
            string topArea = TreeView1.SelectedValue;
            if (topArea == "all")
            {
                foreach (var a in Areas)
                {
                    ShtList.Add(a.AREAID);
                }
            }
            else
            {
                ShtList.Add(short.Parse(topArea));
                foreach (var subArea in Areas.Where(p => p.SubOfArea.ToString() == topArea).Select(p => p))
                {
                    ShtList.Add(subArea.AREAID);
                    recursion(ShtList, subArea.AREAID);
                }
            }
            return ShtList;
        }
        //递归查找子节点
        private void recursion(List<short?> list, short TopID)
        {
            foreach (var sub in Areas.Where(p => p.SubOfArea == TopID).Select(p => p))
            {
                list.Add(sub.AREAID);
                recursion(list, sub.AREAID);
            }
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值