menu控件绑定sql数据库_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > menu控件绑定sql数据库

menu控件绑定sql数据库

 2013/11/16 12:32:22  web李  博客园  我要评论(0)
  • 摘要:1publicinttreeID;23protectedvoidPage_Load(objectsender,EventArgse)4{5SqlConnectionCN=newSqlConnection("DataSource=.;InitialCatalog=TD;IntegratedSecurity=True");6//设定数据库操作命令7CN.Open();8SqlCommandScmd=newSqlCommand("select*fromTDwhereid=0",CN)
  • 标签:数据库 数据 SQL 控件
class="code_img_closed" src="/Upload/Images/2013111612/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('1187d830-83db-4f6f-bc0a-69339bcaba83',event)" src="/Upload/Images/2013111612/2B1B950FA3DF188F.gif" alt="" />
 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

 

发表评论
用户名: 匿名