关于TreeView生成可以选中框的做法

最近搞权限管理这个模块,所以做到用到TreeView这个控件,我在这里用其生成如下的样式

在前台只需要绑定一个TreeView控件

功能项:<asp:TreeView runat="server" ID="TR_Function" Width="40px" ></asp:TreeView>

 

关键是后台代码的实现

  //查出所有的Function项,并将其结果绑定到树形表中。使用递归的方法。
        private void BindTreeView(List<YJPS_HBM_Function> _listFunction)
        {
            List<YJPS_HBM_Function> _fatherFunction = new List<YJPS_HBM_Function>();

            _fatherFunction = _listFunction.Where(d => d.FatherFunctionName == null).ToList();

            foreach (YJPS_HBM_Function _function in _fatherFunction)
            {
                TreeNode tn = new TreeNode();

                tn.Text = _function.FunctionName;

                TR_Function.Nodes.Add(tn);

                tn.ShowCheckBox = true;

                tn.Expanded = false;

                AddChildNode(_listFunction, tn, tn.Text);
            }
        }

        private void AddChildNode(List<YJPS_HBM_Function> _listFunction, TreeNode _tn, string _str)
        {
            List<YJPS_HBM_Function> _childFunction = new List<YJPS_HBM_Function>();

            _childFunction = _listFunction.Where(d => d.FatherFunctionName == _str).ToList();

            foreach (YJPS_HBM_Function _dep in _childFunction)
            {
                TreeNode tc = new TreeNode();

                tc.Text = _dep.FunctionName;

                _tn.ChildNodes.Add(tc);

                tc.ShowCheckBox = true;

                tc.Expanded = false;

                AddChildNode(_listFunction, tc, tc.Text);
            }
        }

        //现在同样用递归的方法,去遍历树形表,获取选中的值。

        private void NavigateTreeView(TreeView _treeView, int _authorityID)
        {
            for (int i = 0; i < _treeView.Nodes.Count; i++)
            {
                if (_treeView.Nodes[i].Checked)
                {
                    YJPS_HBM_AuthorityFunction _authorityFunction = new YJPS_HBM_AuthorityFunction();

                    _authorityFunction.AuthorityID = _authorityID;

                    _authorityFunction.FunctionID = _BLLFunction.GetFunctionByName(_treeView.Nodes[i].Text).ToList()[0].FunctionID;

                    if (Add(_authorityFunction))
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('添加成功')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('添加失败')", true);
                    }

                }
                NavigateTreeNodes(_treeView.Nodes[i], _authorityID);
            }

        }

        private void NavigateTreeNodes(TreeNode _treeNode, int _authorityID)
        {
            for (int i = 0; i < _treeNode.ChildNodes.Count; i++)
            {
                //如果选中 则添加进YJPS_HBM_AuthorityFunction表
                if (_treeNode.ChildNodes[i].Checked)
                {

                    YJPS_HBM_AuthorityFunction _authorityFunction = new YJPS_HBM_AuthorityFunction();

                    _authorityFunction.AuthorityID = _authorityID;

                    _authorityFunction.FunctionID = _BLLFunction.GetFunctionByName(_treeNode.ChildNodes[i].Text).ToList()[0].FunctionID;

                    if (Add(_authorityFunction))
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('添加成功')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "", "alert('添加失败')", true);
                    }
                }

                NavigateTreeNodes(_treeNode.ChildNodes[i], _authorityID);
            }

        }

  

 

原文地址:https://www.cnblogs.com/BuBu/p/2653809.html