递归生成TreeView

本文展示如何利用递归生成Treeview,满足站点导航的需要。废话不多讲,直接上代码。
一.数据库结构

Code
<!-- <br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1 CREATE TABLE [ dbo ] . [ MenuTable ] (
2 [ MenuID ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
3 [ MenuName ] [ nvarchar ] ( 50 )COLLATEChinese_PRC_CI_AS NULL ,
4 [ MenuUrl ] [ nvarchar ] ( 50 )COLLATEChinese_PRC_CI_AS NULL ,
5 [ ParentID ] [ int ] NULL
6 ) ON [ PRIMARY ]


写入几条数据进去 INSERT INTO MenuTable VALUES('网易','http://www.163.com','0')
INSERT INTO MenuTable VALUES('网易邮箱','http://mail.163.com','1')
INSERT INTO MenuTable VALUES('新浪','http://www.sina.com','0')
二.用户控件代码(HTML)

Code
<!-- <br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1 <% @ControlLanguage = " C# " AutoEventWireup = " true " CodeFile = " Navigation.ascx.cs " Inherits = " Navigation " %>
2
3 < asp:TreeView ID ="tvPet" runat ="server" ></ asp:TreeView >


三.后台CS代码

Code
<!-- <br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 using Model;
13 using System.Collections.Generic;
14 using localhost;
15
16 public partial class Navigation:System.Web.UI.UserControl
17 {
18 protected void Page_Load( object sender,EventArgse)
19 {
20 if ( ! IsPostBack)
21 {
22 this .ViewState.Add( " TreeMenuSource " ,GetTreeData());
23 InitTreeView( 0 , null );
24 }
25 }
26
27 /// <summary>
28 /// 递归产生树
29 /// </summary>
30 /// <paramname="pid"></param>
31 /// <paramname="pNode"></param>
32 protected void InitTreeView( int pid,TreeNodepNode)
33 {
34
35 List < MenuTableInfo > listMT = (List < MenuTableInfo > ) this .ViewState[ " TreeMenuSource " ];
36 List < MenuTableInfo > subTreeMenu = listMT.FindAll( delegate (MenuTableInfomt)
37 {
38 if (mt.ParentID == pid)
39 return true ;
40 return false ;
41 });
42
43 foreach (MenuTableInfomInfo in subTreeMenu)
44 {
45 TreeNodenode = new TreeNode();
46 node.Text = mInfo.MenuName;
47 node.Expanded = true ;
48 node.Value = mInfo.MenuID.ToString();
49 node.NavigateUrl = mInfo.MenuUrl;
50
51 if (pNode == null )
52 {
53 this .tvPet.Nodes.Add(node);
54 }
55 else
56 {
57 pNode.ChildNodes.Add(node);
58 }
59 InitTreeView(mInfo.MenuID,node);
60 }
61 }
62
63 /// <summary>
64 /// 获得TreeViewData
65 /// </summary>
66 /// <returns></returns>
67 protected List < MenuTableInfo > GetTreeData()
68 {
69 GetDateFromServerws = new GetDateFromServer();
70 List < MenuTableInfo > listMenuTable = new List < MenuTableInfo > ();
71 DataTabledt = ws.GetMenuTable().Tables[ 0 ];
72
73 foreach (DataRowdr in dt.Rows)
74 {
75 MenuTableInfomt = new MenuTableInfo();
76 mt.MenuID = Convert.ToInt32(dr[ " MenuID " ]);
77 mt.MenuName = dr[ " MenuName " ].ToString();
78 mt.MenuUrl = dr[ " MenuUrl " ].ToString();
79 mt.ParentID = Convert.ToInt32(dr[ " ParentID " ]);
80 listMenuTable.Add(mt);
81 }
82 return listMenuTable;
83 }
84 }
85


四.效果

//设定生成的原始数据 void getdatable() { tblDatas.Columns.Add("groupid", Type.GetType("System.String")); tblDatas.Columns.Add("groupname", Type.GetType("System.String")); tblDatas.Columns.Add("parentid", Type.GetType("System.String")); tblDatas.Rows.Add(new object[] { "1", "机关", "0" }); tblDatas.Rows.Add(new object[] { "2", "学院", "0" }); tblDatas.Rows.Add(new object[] { "3", "教学管理中心", "1" }); tblDatas.Rows.Add(new object[] { "4", "校园管理中心", "1" }); tblDatas.Rows.Add(new object[] { "5", "数据中心", "3" }); tblDatas.Rows.Add(new object[] { "6", "信息中心", "3" }); tblDatas.Rows.Add(new object[] { "7", "一卡通", "4" }); tblDatas.Rows.Add(new object[] { "8", "保卫处", "4" }); tblDatas.Rows.Add(new object[] { "9", "信工系", "2" }); tblDatas.Rows.Add(new object[] { "10", "艺术系", "2" }); dataGridView1.DataSource = tblDatas; } //递归生成函数 public void AddTree(int ParentID, TreeNode pNode) { DataTable dt = new DataTable(); dt = tblDatas; DataView dvTree = new DataView(dt); //过滤ParentID,得到当前的所有子节点 dvTree.RowFilter = "parentid = " + ParentID; foreach (DataRowView Row in dvTree) { TreeNode Node = new TreeNode(); if (pNode == null) { //添加根节点 Node.Text = Row["groupname"].ToString(); treeView1.Nodes.Add(Node); AddTree(Int32.Parse(Row["groupid"].ToString()), Node); //再次递归 } else { //添加当前节点的子节点 Node.Text = Row["groupname"].ToString(); pNode.Nodes.Add(Node); AddTree(Int32.Parse(Row["groupid"].ToString()), Node); //再次递归 } } } //调用递归函数在treeView1里面显示给出数据的形图 private void button1_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); AddTree(0, (TreeNode)null); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值