.NET平台下Web树形结构程序设计
数据库设计
首先,我们在SQL SERVER 2000里建立一个表tbTree,表的结构设计如下:
列名 数据类型 描述 长度 主键
ID Int 节点编号 4 是
ParentID Int 父节点编号 4
ConText Nvarchar 我们要显示的节点内容 50
在SQL SERVER 2000中建表的脚本:
1CREATE TABLE [dbo].[tbTree] (
2 [ID] [int] IDENTITY (1, 1) NOT NULL ,
3 [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
4 [ParentID] [int] NULL
5) ON [PRIMARY]
6
2 [ID] [int] IDENTITY (1, 1) NOT NULL ,
3 [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
4 [ParentID] [int] NULL
5) ON [PRIMARY]
6
在表中添加如下记录:
1SET IDENTITY_INSERT tbtree ON
2insert tbtree (ID,Context,ParentID) values ( 1,'中国',0)
3insert tbtree (ID,Context,ParentID) values ( 2,'北京',11)
4insert tbtree (ID,Context,ParentID) values ( 3,'天津',11)
5insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)
6insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)
7insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)
8insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)
9insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)
10insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)
11insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)
12insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)
13insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)
14insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)
15insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)
16insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)
17SET IDENTITY_INSERT tbtree off
18
19
2insert tbtree (ID,Context,ParentID) values ( 1,'中国',0)
3insert tbtree (ID,Context,ParentID) values ( 2,'北京',11)
4insert tbtree (ID,Context,ParentID) values ( 3,'天津',11)
5insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)
6insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)
7insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)
8insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)
9insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)
10insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)
11insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)
12insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)
13insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)
14insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)
15insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)
16insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)
17SET IDENTITY_INSERT tbtree off
18
19
HTML页:
1 <div>
2 <asp:TreeView ID="TreeView1" runat="server">
3
4 </asp:TreeView>
5 </div>
2 <asp:TreeView ID="TreeView1" runat="server">
3
4 </asp:TreeView>
5 </div>
C#
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12using System.Linq;
13
14
15namespace TreeView
16{
17 public partial class _Default : System.Web.UI.Page
18 {
19 protected DataSet ds = new DataSet();
20 protected void Page_Load(object sender, EventArgs e)
21 {
22 // 定义数据库连接
23 SqlConnection conn = new SqlConnection(@"server=192.168.1.102;database=520tt;uid=sa;pwd=1;");
24 try
25 {
26 conn.Open();
27 SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM dbo.tbTree ",conn);
28 ds = new DataSet();
29 da.Fill(ds);
30 this.ViewState["ds"] = ds;
31 }
32 catch (Exception ex)
33 {
34 Response.Write(ex.Message);
35 }
36 finally
37 {
38 conn.Close();
39 }
40
41 //调用递归函数,完成树形结构的生成
42 AddTree(0, (TreeNode)null);
43
44 }
45
46
47 //递归添加树的节点
48 public void AddTree(int ParentID, TreeNode pNode)
49 {
50 DataView dvTree = new DataView(ds.Tables[0]);
51 //过滤ParentID,得到当前的所有子节点
52 dvTree.RowFilter = "[PARENTID] = " + ParentID;
53
54 foreach (DataRowView Row in dvTree)
55 {
56 TreeNode Node = new TreeNode();
57 if (pNode == null)
58 { //添加根节点
59 Node.Text = Row["ConText"].ToString();
60 TreeView1.Nodes.Add(Node);
61 Node.Expanded = true;
62 AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
63 }
64 else
65 { //̀添加当前节点的子节点
66 Node.Text = Row["ConText"].ToString();
67 pNode.ChildNodes.Add(Node);
68 Node.Expanded = false;
69 AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
70 }
71 }
72 }
73
74 }
75}
76
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12using System.Linq;
13
14
15namespace TreeView
16{
17 public partial class _Default : System.Web.UI.Page
18 {
19 protected DataSet ds = new DataSet();
20 protected void Page_Load(object sender, EventArgs e)
21 {
22 // 定义数据库连接
23 SqlConnection conn = new SqlConnection(@"server=192.168.1.102;database=520tt;uid=sa;pwd=1;");
24 try
25 {
26 conn.Open();
27 SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM dbo.tbTree ",conn);
28 ds = new DataSet();
29 da.Fill(ds);
30 this.ViewState["ds"] = ds;
31 }
32 catch (Exception ex)
33 {
34 Response.Write(ex.Message);
35 }
36 finally
37 {
38 conn.Close();
39 }
40
41 //调用递归函数,完成树形结构的生成
42 AddTree(0, (TreeNode)null);
43
44 }
45
46
47 //递归添加树的节点
48 public void AddTree(int ParentID, TreeNode pNode)
49 {
50 DataView dvTree = new DataView(ds.Tables[0]);
51 //过滤ParentID,得到当前的所有子节点
52 dvTree.RowFilter = "[PARENTID] = " + ParentID;
53
54 foreach (DataRowView Row in dvTree)
55 {
56 TreeNode Node = new TreeNode();
57 if (pNode == null)
58 { //添加根节点
59 Node.Text = Row["ConText"].ToString();
60 TreeView1.Nodes.Add(Node);
61 Node.Expanded = true;
62 AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
63 }
64 else
65 { //̀添加当前节点的子节点
66 Node.Text = Row["ConText"].ToString();
67 pNode.ChildNodes.Add(Node);
68 Node.Expanded = false;
69 AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
70 }
71 }
72 }
73
74 }
75}
76