1 public int treeID; 2 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 SqlConnection CN = new SqlConnection("Data Source=.;Initial Catalog=TD;Integrated Security=True"); 6 //设定数据库操作命令 7 CN.Open(); 8 SqlCommand Scmd = new SqlCommand("select * from TD where id=0", CN); //选出根菜单 9 SqlDataReader odr = Scmd.ExecuteReader(); 10 while (odr.Read()) //循环读取 11 { 12 MenuItem menuNode = new MenuItem(); 13 menuNode.Text = odr["name"].ToString(); 14 menuNode.Value = odr["id"].ToString(); 15 menuNode.Enabled = true; 16 menuNode.NavigateUrl = odr["page"].ToString(); 17 Menu1.Items.Add(menuNode); //添加到根菜单 18 treeID = Convert.ToInt16(odr["id"].ToString()); 19 addchildmenu(menuNode); 20 } 21 //关闭数据库连接 22 Scmd.Connection.Close(); 23 } 24 25 26 27 protected void addchildmenu(MenuItem pnode) //添加子菜单 28 { 29 SqlConnection CN = new SqlConnection("Data Source=.;Initial Catalog=TD;Integrated Security=True"); 30 31 //设定数据库操作命令 32 CN.Open(); 33 SqlCommand Scmd1 = new SqlCommand("select * from TD where id=" + treeID + "", CN); 34 35 SqlDataReader odr = Scmd1.ExecuteReader(); 36 while (odr.Read()) 37 { 38 MenuItem menuNode = new MenuItem(); 39 menuNode.Text = odr["names"].ToString(); 40 menuNode.Value = odr["id"].ToString(); 41 menuNode.Enabled = true; 42 menuNode.NavigateUrl = odr["page"].ToString(); 43 pnode.ChildItems.Add(menuNode); //为当前菜单项添加子菜单 44 treeID = Convert.ToInt16(odr["id"].ToString()); 45 addchildmenu(menuNode); //循环添加子菜单 46 } 47 //关闭数据库连接 48 Scmd1.Connection.Close(); 49 } 50 51 }<asp:Menu ID="Menu1" runat="server" CssClass="dhmenu" Orientation="Horizontal" Width="742px"> 52 <DynamicHoverStyle BackColor="White" /> 53 <DynamicMenuStyle BackColor="#7EBEFA" BorderWidth="1px" CssClass="menuStyleku" /> 54 <DynamicMenuItemStyle CssClass="menuItemSy" ItemSpacing="3px" /> 55 </asp:Menu>View Code