zoukankan      html  css  js  c++  java
  • 利用ASP.NET的menu和SQL数据库制作无限级别菜单

    source:internet

    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;
    using
    WebTools;
    public partial class
    Default4 : System.Web.UI.Page
    {
        
    public int
    parentID;
        
    public Tools Connectionstr1 = new
    Tools();
        
    protected void Page_Load(object
    sender, EventArgs e)
        
    {
            
    string Connectionstr =
    Connectionstr1.connStr.ConnectionString;
             SqlConnection SconnStr
    = new
    SqlConnection(Connectionstr);
            
    //设定数据库操作命令

             SconnStr.Open();
             SqlCommand Scmd
    = new SqlCommand("select * from menuTable where parentID=0"
    , SconnStr);
             SqlDataReader odr
    =
    Scmd.ExecuteReader();
            
    while
    (odr.Read())
            
    {
                 MenuItem menuNode
    = new
    MenuItem();
                 menuNode.Text
    = odr["menuName"
    ].ToString();
                 menuNode.Value
    = odr["id"
    ].ToString();
                 menuNode.Enabled
    = true
    ;
                 menuNode.Target
    = odr["target"
    ].ToString();
                 menuNode.NavigateUrl
    = odr["url"
    ].ToString();
                 Menu1.Items.Add(menuNode);
                 parentID
    = Convert.ToInt16(odr["id"
    ].ToString());
                 addchildmenu1(menuNode, menuNode.Value);
             }

            
    //关闭数据库连接
             Scmd.Connection.Close();
         }

        
    protected void addchildmenu1(MenuItem pnode,string
    pid)
        
    {
            
    string Connectionstr =
    Connectionstr1.connStr.ConnectionString;
             SqlConnection SconnStr1
    = new
    SqlConnection(Connectionstr);
            
    //设定数据库操作命令

             SconnStr1.Open();
             SqlCommand Scmd1
    = new SqlCommand("select * from menuTable where parentID=" + parentID + ""
    , SconnStr1);
             SqlDataReader odr
    =
    Scmd1.ExecuteReader();
            
    while
    (odr.Read())
            
    {
                 MenuItem menuNode
    = new
    MenuItem();
                 menuNode.Text
    = odr["menuName"
    ].ToString();
                 menuNode.Value
    = odr["id"
    ].ToString();
                 menuNode.Enabled
    = true
    ;
                 menuNode.Target
    = odr["target"
    ].ToString();
                 menuNode.NavigateUrl
    = odr["url"
    ].ToString();
                 pnode.ChildItems.Add(menuNode);
                 parentID
    = Convert.ToInt16(odr["id"
    ].ToString());
                 addchildmenu2(menuNode, menuNode.Value);
             }

            
    //关闭数据库连接
             Scmd1.Connection.Close();
         }

        
    protected void addchildmenu2(MenuItem pnode, string
    pid)
        
    {
            
    string Connectionstr =
    Connectionstr1.connStr.ConnectionString;
             SqlConnection SconnStr1
    = new
    SqlConnection(Connectionstr);
            
    //设定数据库操作命令

             SconnStr1.Open();
             SqlCommand Scmd1
    = new SqlCommand("select * from menuTable where parentID=" + parentID + ""
    , SconnStr1);
             SqlDataReader odr
    =
    Scmd1.ExecuteReader();
            
    while
    (odr.Read())
            
    {
                 MenuItem menuNode
    = new
    MenuItem();
                 menuNode.Text
    = odr["menuName"
    ].ToString();
                 menuNode.Value
    = odr["id"
    ].ToString();
                 menuNode.Enabled
    = true
    ;
                 menuNode.Target
    = odr["target"
    ].ToString();
                 menuNode.NavigateUrl
    = odr["url"
    ].ToString();
                 pnode.ChildItems.Add(menuNode);
                 parentID
    = Convert.ToInt16(odr["id"
    ].ToString());
                 addchildmenu1(menuNode, menuNode.Value);
             }

            
    //关闭数据库连接
             Scmd1.Connection.Close();
  • 相关阅读:
    几个带双下划线的宏
    WM_COPYDATA消息机制 不同进程间发送结构体数据
    解锁ubuntu系统的root
    申请堆空间函数封装(两种方法)
    UITableView的分页的加载
    UIView设置成圆角
    iPhone开发:类似iChat的聊天泡泡
    iPhone中如何自定义tabbar
    android 底部选项卡(TabHost)
    Android 多个Activity选项卡实现
  • 原文地址:https://www.cnblogs.com/NetSos/p/1749614.html
Copyright © 2011-2022 走看看