利用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();

你可能感兴趣的:(asp.net)