ASP.NET 递归算法 生成N级(级联)树TreeView
HTML代码:
view plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" Height="257px" ImageSet="WindowsHelp"
Width="142px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="1px" />
</asp:TreeView>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" Height="257px" ImageSet="WindowsHelp"
Width="142px">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="1px" />
</asp:TreeView>
</div>
</form>
</body>
</html>
后台代码:
view plaincopy to clipboardprint?
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;
public partial class _Default : System.Web.UI.Page
{
DataView dv;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//数据库执行查询的方法就不写了。
Operator op = new Operator();
//这里执行的是:select * from tabmenu
dt = op.Query();
//第一次加载时调用方法传参
CreateTree(0, null, dt, this.TreeView1);
this.TreeView1.ExpandAll();
}
}
/// <summary>
/// 创建一个树
/// </summary>
/// <param name="parentID">父ID</param>
/// <param name="node">节点</param>
/// <param name="dt">DataTable</param>
/// <param name="treeView">TreeView的名称</param>
public void CreateTree(int parentID, TreeNode node, DataTable dt, TreeView treeView)
{
//实例化一个DataView dt = 传入的DataTable
dv = new DataView(dt);
//筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID)
dv.RowFilter = "[menuParentID]=" + parentID;
//用foreach遍历dv
foreach (DataRowView row in dv)
{
//第一次加载时为空
if (node == null)
{
//创建根节点
TreeNode root = new TreeNode();
//必须与数据库的对应
root.Text = row["menuName"].ToString();
root.Value = row["menuID"].ToString();
//添加根节点
this.TreeView1.Nodes.Add(root);
//递归调用方法本身
CreateTree(int.Parse(row["menuID"].ToString()), root, dt, treeView);
}
else
{
//添加子节点
TreeNode childNode = new TreeNode();
childNode.Text = row["menuName"].ToString();
childNode.Value = row["menuID"].ToString();
node.ChildNodes.Add(childNode);
CreateTree(int.Parse(row["menuID"].ToString()), childNode, dt, treeView);
}
}
}
}
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;
public partial class _Default : System.Web.UI.Page
{
DataView dv;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//数据库执行查询的方法就不写了。
Operator op = new Operator();
//这里执行的是:select * from tabmenu
dt = op.Query();
//第一次加载时调用方法传参
CreateTree(0, null, dt, this.TreeView1);
this.TreeView1.ExpandAll();
}
}
/// <summary>
/// 创建一个树
/// </summary>
/// <param name="parentID">父ID</param>
/// <param name="node">节点</param>
/// <param name="dt">DataTable</param>
/// <param name="treeView">TreeView的名称</param>
public void CreateTree(int parentID, TreeNode node, DataTable dt, TreeView treeView)
{
//实例化一个DataView dt = 传入的DataTable
dv = new DataView(dt);
//筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID)
dv.RowFilter = "[menuParentID]=" + parentID;
//用foreach遍历dv
foreach (DataRowView row in dv)
{
//第一次加载时为空
if (node == null)
{
//创建根节点
TreeNode root = new TreeNode();
//必须与数据库的对应
root.Text = row["menuName"].ToString();
root.Value = row["menuID"].ToString();
//添加根节点
this.TreeView1.Nodes.Add(root);
//递归调用方法本身
CreateTree(int.Parse(row["menuID"].ToString()), root, dt, treeView);
}
else
{
//添加子节点
TreeNode childNode = new TreeNode();
childNode.Text = row["menuName"].ToString();
childNode.Value = row["menuID"].ToString();
node.ChildNodes.Add(childNode);
CreateTree(int.Parse(row["menuID"].ToString()), childNode, dt, treeView);
}
}
}
}
数据库代码:
view plaincopy to clipboardprint?
create table tabMenu
(
menuID int identity(1,1) primary key,--自动编号
menuParentID int not null,--上级菜单的编号
menuName varchar(20) not null,--菜单名称
menuUrl varchar(20) not null,--菜单地址
menuImage varchar(20) not null, --图片地址
menuTarget varchar(20) not null,--打开方式
)
go
--0表示根节点
insert into tabMenu
values(0,'xx后台管理','','','')
--1子节点
insert into tabMenu
values(1,'学生管理','','','')
insert into tabMenu
values(1,'班级管理','','','')
--2级节点
insert into tabMenu
values(2,'S1','','','')
insert into tabMenu
values(2,'S2','','','')
insert into tabMenu
values(2,'s3','','','')
--3级节点
insert into tabMenu
values(3,'S1','','','')
insert into tabMenu
values(3,'S2','','','')
insert into tabMenu
values(3,'s3','','','')
insert into tabMenu
values(4,'Ccc','','','')
insert into tabMenu
values(5,'bbb,'','','')
insert into tabMenu
values(6,'fff','','','')
--
insert into tabMenu
values(7,'stu1','','','')
insert into tabMenu
values(8,'stu2','','','')
insert into tabMenu
values(9,'stu1','','','')
select * from tabMenu
go