具體方法是:創建一個數據庫,設計樹圖信息表S_Menu,包含NodeId、ParentId、NodeName、Address、Icon字段,其它字段根據實際業務而定,節點名稱NodeName將在樹型控件的節點上顯示,NodeId字段保存節點的唯一標識號,ParentId表示當前節點的父節點號,標識號組成了一個“鏈表”,記錄了樹上節點的結構。設計一個Web窗體其上放置TreeView控件,修改其屬性Id為tvMenu。
數據結構如下:
CREATE TABLE [dbo].[S_Menu] ( [NodeId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL , [ParentId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL , [NodeName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Address] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Icon] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] GO
數據庫如下:
NodeId ParentId NodeName Address Icon 100000 0 公共查詢部 icon_document.gif 100001 100000 人民幣日報查詢 public/a1.aspx icon_settings.gif 100002 100000 外幣日報查詢 public/a2.aspx icon_settings.gif 100003 0 分行科技部 icon_document.gif 100004 100003 人民幣日報查詢 tech/a1.aspx icon_settings.gif 100005 100003 外幣日報查詢 tech/a2.aspx icon_settings.gif 100006 0 福田支行 icon_document.gif 100007 100006 月存款進度表 a1.aspx icon_settings.gif 100008 100006 月存款走勢圖 a2.aspx icon_settings.gif 100009 0 羅湖支行 icon_document.gif 100010 100009 月存款進度表 a1.aspx icon_settings.gif 100011 100009 月存款走勢圖 a2.aspx icon_settings.gif menu_left.aspx文件如下: <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Page language="c#" Codebehind="menu_Left.aspx.cs" AutoEventWireup="false" Inherits="hzquery.menu.menu_Left" %> <HTML> <HEAD> <title>menu_Left</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="menu_Left" method="post" runat="server"> <iewc:TreeView id="tvMenu" runat="server"></iewc:TreeView> </form> </body> </HTML> CodeBehind代碼如下: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using Microsoft.Web.UI.WebControls; using System.Data.SqlClient; namespace hzquery.menu { /// <summary> /// menu_Left 的摘要說明。 /// </summary> public class menu_Left : System.Web.UI.Page { protected Microsoft.Web.UI.WebControls.TreeView tvMenu; SqlConnection Conn; SqlDataAdapter myCmd; DataSet ds; string cmdSelect; private void Page_Load(object sender, System.EventArgs e) { Conn=new SqlConnection(Application["ConnString"].ToString()); CreateDataSet(); InitTree(tvMenu.Nodes,"0"); } //建立數據集 private DataSet CreateDataSet() { cmdSelect="select * from S_Menu"; myCmd=new SqlDataAdapter(cmdSelect,Conn); ds=new DataSet(); myCmd.Fill(ds,"tree"); return ds; } //建樹的基本思路是:從根節點開始遞歸調用顯示子樹 private void InitTree(TreeNodeCollection Nds,string parentId) { DataView dv=new DataView(); TreeNode tmpNd; string intId; dv.Table=ds.Tables["tree"]; dv.RowFilter="ParentId="" + parentId + """ ; foreach(DataRowView drv in dv) { tmpNd=new TreeNode(); tmpNd.ID=drv["NodeId"].ToString(); tmpNd.Text=drv["NodeName"].ToString(); tmpNd.ImageUrl="../images/"+drv["Icon"].ToString(); tmpNd.NavigateUrl="../"+drv["Address"].ToString(); Nds.Add(tmpNd); intId=drv["ParentId"].ToString(); InitTree(tmpNd.Nodes,tmpNd.ID); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }