具体文件: E:\学习\treeview\Solution1
效果如下:
效果如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="tvMenu" runat="server" ImageSet="BulletedList4" ShowLines="True" ExpandDepth="1" Target="middle" Height="324px" OnLoad="tvMenu_Load" Width="385px" >
<ParentNodeStyle Font-Bold="False" ForeColor ="Red" />
<HoverNodeStyle Font-Underline="True" ForeColor="Blue" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor = "Green" ImageUrl="~/Images/bt_mline_on.gif"/>
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
<RootNodeStyle BorderStyle="Dashed" />
</asp:TreeView>
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="tvMenu" runat="server" ImageSet="BulletedList4" ShowLines="True" ExpandDepth="1" Target="middle" Height="324px" OnLoad="tvMenu_Load" Width="385px" >
<ParentNodeStyle Font-Bold="False" ForeColor ="Red" />
<HoverNodeStyle Font-Underline="True" ForeColor="Blue" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor = "Green" ImageUrl="~/Images/bt_mline_on.gif"/>
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
<RootNodeStyle BorderStyle="Dashed" />
</asp:TreeView>
</form>
</body>
</html>
cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
public partial class treeviewdg : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection("server=A3161228A954417;database=SMS;uid=sa;pwd=sa");
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
}
private DataSet createDataSet()
{
ds = new DataSet();
string sqlStr = "select * from Tree ";
SqlDataAdapter cmdSelect = new SqlDataAdapter(sqlStr, Conn);
cmdSelect.Fill(ds, "Tree");
return ds;
}
protected void InitTree(TreeNodeCollection Nds, string parentId)//用递归方法动态生成节点
{
DataView dv = new DataView();
TreeNode tmpNode;
dv.Table = ds.Tables["Tree"];
dv.RowFilter = "pat_id=" + "'" + parentId + "'";
foreach (DataRowView drv in dv)
{
tmpNode = new TreeNode();
tmpNode.Value = drv["node_id"].ToString();
tmpNode.Text = drv["node_name"].ToString();
tmpNode.NavigateUrl = drv["url"].ToString();
Nds.Add(tmpNode);
this.InitTree(tmpNode.ChildNodes, tmpNode.Value);
}
}
protected void tvMenu_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tvMenu.Dispose();
Conn.Open();
this.createDataSet();
Conn.Close();
this.InitTree(tvMenu.Nodes, "0");
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
public partial class treeviewdg : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection("server=A3161228A954417;database=SMS;uid=sa;pwd=sa");
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
}
private DataSet createDataSet()
{
ds = new DataSet();
string sqlStr = "select * from Tree ";
SqlDataAdapter cmdSelect = new SqlDataAdapter(sqlStr, Conn);
cmdSelect.Fill(ds, "Tree");
return ds;
}
protected void InitTree(TreeNodeCollection Nds, string parentId)//用递归方法动态生成节点
{
DataView dv = new DataView();
TreeNode tmpNode;
dv.Table = ds.Tables["Tree"];
dv.RowFilter = "pat_id=" + "'" + parentId + "'";
foreach (DataRowView drv in dv)
{
tmpNode = new TreeNode();
tmpNode.Value = drv["node_id"].ToString();
tmpNode.Text = drv["node_name"].ToString();
tmpNode.NavigateUrl = drv["url"].ToString();
Nds.Add(tmpNode);
this.InitTree(tmpNode.ChildNodes, tmpNode.Value);
}
}
protected void tvMenu_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tvMenu.Dispose();
Conn.Open();
this.createDataSet();
Conn.Close();
this.InitTree(tvMenu.Nodes, "0");
}
}
}
脚本:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tree]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tree]
GO
CREATE TABLE [dbo].[tree] (
[node_id] [int] NOT NULL ,
[node_name] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[pat_id] [int] NULL ,
[url] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[icon] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[memo] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
drop table [dbo].[tree]
GO
CREATE TABLE [dbo].[tree] (
[node_id] [int] NOT NULL ,
[node_name] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[pat_id] [int] NULL ,
[url] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[icon] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[memo] [varchar] (30) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
数据如下
1 根结点 0 # NULL NULL
2 子结点2 1 # NULL NULL
3 子结点3 1 # NULL NULL
4 子结点4 1 # NULL NULL
5 结点2-1 2 Default.aspx NULL NULL
6 结点2-2 2 # NULL NULL
7 结点3-1 3 # NULL NULL
8 结点3-2 3 # NULL NULL
2 子结点2 1 # NULL NULL
3 子结点3 1 # NULL NULL
4 子结点4 1 # NULL NULL
5 结点2-1 2 Default.aspx NULL NULL
6 结点2-2 2 # NULL NULL
7 结点3-1 3 # NULL NULL
8 结点3-2 3 # NULL NULL