• 利用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();
  • 相关阅读:
    牛客挑战赛48C铬合金之声【Prufer序列】
    Java 基础 反射
    MDX Query mdx的基本语法和概念
    Maven 深入理解maven构建生命周期和各种plugin插件
    Java基础 String,StringBuilder,StringBuffer三者的区别
    Java基础 Java 抽象类 抽象方法
    Java 基础 final vs static
    JMX JMX(Java Management Extensions)定义
    Java 多线程 生产者消费者问题
    Java 基础 如何重写equals()
  • 原文地址:https://www.cnblogs.com/NetSos/p/1749614.html
Copyright © 2020-2023  润新知