menu控件绑定sql数据库

2018-06-17 21:08:48来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Js获取Gridview中Dropdownlist选中状态

下一篇:SandDock 应用示例