asp.net TreeView递归循环子节点生成树形菜单实例

这里主要用到递归循环获取子结点

/// <summary> /// 生成根节点 /// </summary> /// <param></param> protected void BindTreeView(long ID, TreeView treeview) { DataTable dt = menuLogic.GetMenu2(ID, User.Identity.Name); treeview.Nodes.Clear(); DataRow[] parentrow = dt.Select("par_id=0"); for (int i = 0; i < parentrow.Length; i++) { TreeNode rootnode = new TreeNode(); rootnode.Text = parentrow[i]["node_name"].ToString(); //parentrow[i][3].ToString(); rootnode.Value = parentrow[i]["ID"].ToString(); //parentrow[i][1].ToString(); 主键 rootnode.Expanded = true; rootnode.Selected = false; rootnode.SelectAction = TreeNodeSelectAction.None; treeview.Nodes.Add(rootnode); CreateChildNode(rootnode, dt); // } } /// <summary> /// 生成子节点 /// </summary> /// <param></param> /// <param></param> protected void CreateChildNode(TreeNode parentNode, DataTable datatable) { DataRow[] rowlist = datatable.Select("par_id=" + parentNode.Value); for (int i = 0; i < rowlist.Length; i++) { TreeNode node = new TreeNode(); if (datatable.Select("par_id=" + rowlist[i]["ID"].ToString().Trim()).Length > 0) { node.Text = rowlist[i]["node_name"].ToString(); node.Value = rowlist[i]["ID"].ToString(); } else { node.Text = "<a href=https://www.jb51.net/article/\"javascript:;\" onclick=https://www.jb51.net/article/\"OpenUrl('" + rowlist[i]["url"].ToString().Trim() + "',this)\">" + rowlist[i]["node_name"].ToString() + "</a>"; node.Value = rowlist[i]["ID"].ToString(); } node.Expanded = false; node.Selected = false; node.SelectAction = TreeNodeSelectAction.None; parentNode.ChildNodes.Add(node); CreateChildNode(node, datatable); //递归调用 } }

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyfxg.html