ASP.NET下的TreeView控件的使用(生成树与统计所有子节点数量)
View Code
1 //GetAllNodeText(trvPlacement.Nodes); //统计TreeView所有子节点数量
2
3 private void GetAllNodeText(TreeNodeCollection tnc) //统计TreeView所有子节点数量
4 {
5 foreach (TreeNode node in tnc)
6 {
7 if (node.ChildNodes.Count != 0)
8 {
9 GetAllNodeText(node.ChildNodes);
10 }
11 node.Text = node.Text + GetNodesCount(node);
12 }
13 }
14
15 private int GetNodesCount(TreeNode tnc) //统计TreeView节点下所有子节点数量
16 {
17 int intCount = tnc.ChildNodes.Count;
18 if (tnc.ChildNodes.Count > 0)
19 {
20 foreach (TreeNode node in tnc.ChildNodes)
21 {
22 intCount = intCount + GetNodesCount(node);
23 }
24 }
25 return intCount;
26 }
27
28 private void InitTree(TreeNode Nd, String Parent_id) //子树节点加载函数
29 {
30 DataRow[] rows = dtbUserAllInfo.Select("Parent=" + Parent_id);
31 if (rows != null)
32 {
33 for (int i = 0; i < rows.Length; i++)
34 {
35 TreeNode Tnd = new TreeNode();
36 DataRow dr = rows[i];
37 Tnd.Text = dr["Name"].ToString();
38
39 intPeopleNum += 1;
40 Nd.ChildNodes.Add(Tnd);
41 InitTree(Tnd, dr["ID"].ToString());//递归调用
42 }
43 }
44 }
2
3 private void GetAllNodeText(TreeNodeCollection tnc) //统计TreeView所有子节点数量
4 {
5 foreach (TreeNode node in tnc)
6 {
7 if (node.ChildNodes.Count != 0)
8 {
9 GetAllNodeText(node.ChildNodes);
10 }
11 node.Text = node.Text + GetNodesCount(node);
12 }
13 }
14
15 private int GetNodesCount(TreeNode tnc) //统计TreeView节点下所有子节点数量
16 {
17 int intCount = tnc.ChildNodes.Count;
18 if (tnc.ChildNodes.Count > 0)
19 {
20 foreach (TreeNode node in tnc.ChildNodes)
21 {
22 intCount = intCount + GetNodesCount(node);
23 }
24 }
25 return intCount;
26 }
27
28 private void InitTree(TreeNode Nd, String Parent_id) //子树节点加载函数
29 {
30 DataRow[] rows = dtbUserAllInfo.Select("Parent=" + Parent_id);
31 if (rows != null)
32 {
33 for (int i = 0; i < rows.Length; i++)
34 {
35 TreeNode Tnd = new TreeNode();
36 DataRow dr = rows[i];
37 Tnd.Text = dr["Name"].ToString();
38
39 intPeopleNum += 1;
40 Nd.ChildNodes.Add(Tnd);
41 InitTree(Tnd, dr["ID"].ToString());//递归调用
42 }
43 }
44 }