aspx: < asp:Menu ID = "tvMenu" runat ="server" > < StaticMenuStyle BackColor ="Silver" /> < StaticMenuItemStyle BackColor ="LightSteelBlue" /> < DynamicHoverStyle BackColor ="Fuchsia" /> < StaticSelectedStyle BackColor ="Red" /> </ asp:Menu > cs: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Menutest : System.Web.UI.Page { SqlConnection Conn = new SqlConnection("server=A3161228A954417;database=SMS;uid=sa;pwd=sa"); DataSet ds; protected void Page_Load(object sender, EventArgs e) { } private DataSet createDataSet() { ds = new DataSet(); string sqlStr = "select * from Tree "; SqlDataAdapter cmdSelect = new SqlDataAdapter(sqlStr, Conn); cmdSelect.Fill(ds, "Tree"); return ds; } protected void InitTree(MenuItemCollection Nds, string parentId)//用递归方法动态生成节点 { DataView dv = new DataView(); MenuItem tmpNode; dv.Table = ds.Tables["Tree"]; dv.RowFilter = "pat_id=" + "'" + parentId + "'"; foreach (DataRowView drv in dv) { tmpNode = new MenuItem(); tmpNode.Value = drv["node_id"].ToString(); tmpNode.Text = drv["node_name"].ToString(); tmpNode.NavigateUrl = drv["url"].ToString(); Nds.Add(tmpNode); this.InitTree(tmpNode.ChildItems, tmpNode.Value); } } protected void tvMenu_Load(object sender, EventArgs e) { if (!IsPostBack) { tvMenu.Dispose(); Conn.Open(); this.createDataSet(); Conn.Close(); this.InitTree(tvMenu.Items, "0"); } }} 脚本: if exists ( select * from dbo.sysobjects where id = object_id (N ' [dbo].[tree] ' ) and OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 ) drop table [ dbo ] . [ tree ] GO CREATE TABLE [ dbo ] . [ tree ] ( [ node_id ] [ int ] NOT NULL , [ node_name ] [ varchar ] ( 20 ) COLLATE Chinese_PRC_CI_AS NULL , [ pat_id ] [ int ] NULL , [ url ] [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS NULL , [ icon ] [ varchar ] ( 20 ) COLLATE Chinese_PRC_CI_AS NULL , [ memo ] [ varchar ] ( 30 ) COLLATE Chinese_PRC_CI_AS NULL ) ON [ PRIMARY ] GO 数据如下:(说明:用#可消除刷新) 1 根结点 0 # NULL NULL 2 子结点2 1 # NULL NULL 3 子结点3 1 # NULL NULL 4 子结点4 1 # NULL NULL 5 结点2 - 1 2 Default .aspx NULL NULL 6 结点2 - 2 2 # NULL NULL 7 结点3 - 1 3 # NULL NULL 8 结点3 - 2 3 # NULL NULL