DevExpress中的ASPxTreeView 递归显示checknodes并获得选中值

时间:2021-07-10 09:03:28

aspx代码

<dx:ASPxTreeView ID="ASPxTreeView1" runat="server">
             </dx:ASPxTreeView>

aspx.cs代码

     protected void Page_Load(object sender, EventArgs e)
        {
            AddTree(, null);

        }
        private void AddTree(int Pid, TreeViewNode PNode)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string strSQL =
                "SELECT * FROM test";
            System.Diagnostics.Debug.WriteLine(strSQL);

            DataSet ds = sqlHelper.ExecuteDataSet(strSQL);
            DataTable dt = ds.Tables[];

            )
            {
                DataView dv = new DataView(dt);
                //过滤ParentID,得到当前的所有子节点 ParentID为父节点ID
                dv.RowFilter = "[duty_parentid] = " + Pid;
                //循环递归
                foreach (DataRowView Row in dv)
                {
                    //声明节点
                    TreeViewNode Node = new TreeViewNode();
                    //绑定超级链接
                    Node.NavigateUrl = String.Format("javascript:show('{0}')", Row["dutyName"].ToString());
                    //开始递归
                    if (PNode == null)
                    {
                        //添加根节点
                        Node.Text = Row["dutyName"].ToString();
                        ASPxTreeView1.Nodes.Add(Node);
                        Node.Expanded = false; //节点状态展开
                        AddTree(Int32.Parse(Row["dutyID"].ToString()), Node);    //再次递归
                    }
                    else
                    {
                        //添加当前节点的子节点
                        Node.Text = Row["dutyName"].ToString();
                        PNode.Nodes.Add(Node);
                        Node.Expanded = false; //节点状态展开
                        AddTree(Int32.Parse(Row["dutyID"].ToString()), Node);     //再次递归
                    }
                }
            }
        }

获取选中的Nodes

private List<TreeViewNode> GetSelectNodes(TreeViewNodeCollection Nodes)
        {

            List<TreeViewNode> listNodes = new List<TreeViewNode>();
            foreach (TreeViewNode node in Nodes)
            {
                FindCheckNode(node, listNodes);
            }          

            return listNodes;

        }

        private void FindCheckNode(TreeViewNode node, List<TreeViewNode> listNodes)
        {
            if (node.Checked)
            {
                listNodes.Add(node);
            }
            foreach (TreeViewNode childnode in node.Nodes)
            {
                FindCheckNode(childnode, listNodes);
            }
        }