1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 public partial class _Default : System.Web.UI.Page
12 {
13 private void Page_Load( object sender, System.EventArgs e)
14 {
15 // 定义数据库连接
16 SqlConnection conn = new SqlConnection();
17 // 初始化连接字符串
18 conn.ConnectionString = " data source=.;initial catalog=mybookshop;uid=sa;pwd=sa; " ;
19 conn.Open();
20 SqlDataAdapter da = new SqlDataAdapter( " select * from NodeInfo " ,conn);
21 DataSet ds = new DataSet ();
22 da.Fill(ds);
23 this .ViewState[ " ds " ] = ds;
24 conn.Close();
25 // 调用递归函数,完成树形结构的生成
26 AddTree( 0 , (TreeNode) null );
27 }
28
29 // 递归添加树的节点
30 public void AddTree( int parentNodeId, TreeNode pNode)
31 {
32 DataSet ds = (DataSet) this .ViewState[ " ds " ];
33 DataView dvTree = new DataView(ds.Tables[ 0 ]);
34 // 过滤parentNodeId,得到当前的所有子节点
35 dvTree.RowFilter = " [parentNodeId] = " + parentNodeId;
36
37 foreach (DataRowView Row in dvTree)
38 {
39 TreeNode Node = new TreeNode();
40 if (pNode == null )
41 {
42 // 添加根节点
43 Node.Text = Row[ " nodeText " ].ToString();
44 TreeView1.Nodes.Add(Node);
45 Node.Expanded = true ;
46 AddTree( int .Parse(Row[ " nodeId " ].ToString()), Node); // 再次递归
47 }
48 else
49 {
50 // 添加当前节点的子节点
51 Node.Text = Row[ " nodeText " ].ToString();
52 pNode.ChildNodes.Add(Node);
53 Node.Expanded = true ;
54 AddTree( int .Parse(Row[ " nodeId " ].ToString()), Node); // 再次递归
55 }
56 }
57 }
58
59 }
60
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 public partial class _Default : System.Web.UI.Page
12 {
13 private void Page_Load( object sender, System.EventArgs e)
14 {
15 // 定义数据库连接
16 SqlConnection conn = new SqlConnection();
17 // 初始化连接字符串
18 conn.ConnectionString = " data source=.;initial catalog=mybookshop;uid=sa;pwd=sa; " ;
19 conn.Open();
20 SqlDataAdapter da = new SqlDataAdapter( " select * from NodeInfo " ,conn);
21 DataSet ds = new DataSet ();
22 da.Fill(ds);
23 this .ViewState[ " ds " ] = ds;
24 conn.Close();
25 // 调用递归函数,完成树形结构的生成
26 AddTree( 0 , (TreeNode) null );
27 }
28
29 // 递归添加树的节点
30 public void AddTree( int parentNodeId, TreeNode pNode)
31 {
32 DataSet ds = (DataSet) this .ViewState[ " ds " ];
33 DataView dvTree = new DataView(ds.Tables[ 0 ]);
34 // 过滤parentNodeId,得到当前的所有子节点
35 dvTree.RowFilter = " [parentNodeId] = " + parentNodeId;
36
37 foreach (DataRowView Row in dvTree)
38 {
39 TreeNode Node = new TreeNode();
40 if (pNode == null )
41 {
42 // 添加根节点
43 Node.Text = Row[ " nodeText " ].ToString();
44 TreeView1.Nodes.Add(Node);
45 Node.Expanded = true ;
46 AddTree( int .Parse(Row[ " nodeId " ].ToString()), Node); // 再次递归
47 }
48 else
49 {
50 // 添加当前节点的子节点
51 Node.Text = Row[ " nodeText " ].ToString();
52 pNode.ChildNodes.Add(Node);
53 Node.Expanded = true ;
54 AddTree( int .Parse(Row[ " nodeId " ].ToString()), Node); // 再次递归
55 }
56 }
57 }
58
59 }
60
1
using
System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Data.SqlClient;
14
15 namespace TreeViewAndMenuDemo
16 {
17 public partial class _Default : System.Web.UI.Page
18 {
19 protected void Page_Load( object sender, EventArgs e)
20 {
21 if ( ! IsPostBack)
22 {
23 // BindTreeView();
24 }
25 }
26 ///
27 /// 绑定树控件
28 ///
29 public void BindTreeView()
30 {
31 string strcon = ConfigurationManager.ConnectionStrings[ " sql " ].ConnectionString;
32 using (SqlConnection con = new SqlConnection(strcon))
33 {
34 string sql = " select * from Table " ;
35 SqlDataAdapter da = new SqlDataAdapter(sql, con);
36 DataSet ds = new DataSet();
37 da.Fill(ds);
38 ViewState[ " ds " ] = ds;
39 BindRoot(ds);
40 }
41 }
42 ///
43 /// 绑定根节点
44 ///
45 ///
46 private void BindRoot(DataSet ds)
47 {
48 TreeNode root = new TreeNode();
49 // 获得最外层节点
50 DataRow[] dr = ds.Tables[ 0 ].Select( " fatherNode=0 " );
51 foreach (DataRow r in dr)
52 {
53 TreeNode tn = new TreeNode();
54 tn.Text = r[ 2 ].ToString();
55 tn.Target = r[ 1 ].ToString();
56 root.ChildNodes.Add(tn);
57 // 调用添加子节点方法
58 // AddChildNode(ds,tn);
59 }
60 TreeView1.Nodes.Add(root);
61 TreeView1.ExpandAll();
62 }
63 ///
64 /// 子节点添加方法
65 ///
66 /// 数据集
67 /// 当前节点对象
68 private void AddChildNode(DataSet ds, TreeNode tn)
69 {
70 DataRow[] dr = ds.Tables[ 0 ].Select( " fatherNode= " + tn.Target);
71 foreach (DataRow r in dr)
72 {
73 TreeNode tc = new TreeNode();
74 tc.Text = r[ 2 ].ToString();
75 tc.Target = r[ 1 ].ToString();
76 tn.ChildNodes.Add(tc);
77 // 递归调用
78 // AddChildNode(ds, tc);
79 }
80 }
81 }
82 }
83
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Data.SqlClient;
14
15 namespace TreeViewAndMenuDemo
16 {
17 public partial class _Default : System.Web.UI.Page
18 {
19 protected void Page_Load( object sender, EventArgs e)
20 {
21 if ( ! IsPostBack)
22 {
23 // BindTreeView();
24 }
25 }
26 ///
27 /// 绑定树控件
28 ///
29 public void BindTreeView()
30 {
31 string strcon = ConfigurationManager.ConnectionStrings[ " sql " ].ConnectionString;
32 using (SqlConnection con = new SqlConnection(strcon))
33 {
34 string sql = " select * from Table " ;
35 SqlDataAdapter da = new SqlDataAdapter(sql, con);
36 DataSet ds = new DataSet();
37 da.Fill(ds);
38 ViewState[ " ds " ] = ds;
39 BindRoot(ds);
40 }
41 }
42 ///
43 /// 绑定根节点
44 ///
45 ///
46 private void BindRoot(DataSet ds)
47 {
48 TreeNode root = new TreeNode();
49 // 获得最外层节点
50 DataRow[] dr = ds.Tables[ 0 ].Select( " fatherNode=0 " );
51 foreach (DataRow r in dr)
52 {
53 TreeNode tn = new TreeNode();
54 tn.Text = r[ 2 ].ToString();
55 tn.Target = r[ 1 ].ToString();
56 root.ChildNodes.Add(tn);
57 // 调用添加子节点方法
58 // AddChildNode(ds,tn);
59 }
60 TreeView1.Nodes.Add(root);
61 TreeView1.ExpandAll();
62 }
63 ///
64 /// 子节点添加方法
65 ///
66 /// 数据集
67 /// 当前节点对象
68 private void AddChildNode(DataSet ds, TreeNode tn)
69 {
70 DataRow[] dr = ds.Tables[ 0 ].Select( " fatherNode= " + tn.Target);
71 foreach (DataRow r in dr)
72 {
73 TreeNode tc = new TreeNode();
74 tc.Text = r[ 2 ].ToString();
75 tc.Target = r[ 1 ].ToString();
76 tn.ChildNodes.Add(tc);
77 // 递归调用
78 // AddChildNode(ds, tc);
79 }
80 }
81 }
82 }
83