多级下拉菜单 (小山)


-------------------------------------
源代码下载/Files/singlepine/topmenu.rar

public   class  TopMenu : System.Web.UI.Page
{
    
protected DataRow[] father;
    
protected DataRow[] first;
    
protected DataRow[] second;
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
        }

    
public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
    
        
#region GetDataSet
        
public static DataSet GetDataSet(string sql)
        
{
            SqlDataAdapter    sda 
=new SqlDataAdapter(sql,ConnectionString);
            DataSet ds
=new DataSet();
            sda.Fill(ds);
            
return ds;
        }

        
#endregion

        
#region Web Form Designer generated code
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/**//// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
        
{    
            
this.Load += new System.EventHandler(this.Page_Load);
        }

        
#endregion

    
#region LoadTopMenu
    
protected string LoadTopMenu()
    
{
        
//IsBoot设置菜单级别,0一级,1二级,2三级,依此类推.
        string sqlFather="select * from topmenu order by IsBoot";
        DataSet dsFather
=GetDataSet(sqlFather);
        father
=dsFather.Tables[0].Select("IsBoot=0","IsBoot");
        
string menu="";            
        
int one=0;
        
int two=1;
        
int three=1;
        
foreach(DataRow drfather in father)
        
{                
            menu
+="mpmenu"+one+"=new mMenu("+"'"+drfather["text"]+"'"+",'/','self','','','','');";
            first
=dsFather.Tables[0].Select("ParentID='"+Convert.ToInt32(drfather["ID"])+"' and IsBoot=1","IsBoot");
            
foreach(DataRow drfirst in first)
            
{
                second
=dsFather.Tables[0].Select("ParentID='"+Convert.ToInt32(drfirst["ID"])+"' and IsBoot=2","IsBoot");
                
if(second.Length==0)
                
{
                    menu
+="mpmenu"+one+".addItem(new mMenuItem("+"'"+drfirst["text"]+"'";//description
                    menu+=","+"'"+drfirst["url"]+"'"+",";//url
                    menu+="'"+drfirst["target"]+"'"+",";//target
                    menu+=""+drfirst["visible"]+",";//是否可见,false可见,true不可见
                    menu+="'"+drfirst["status"]+"'"+",";//状态条
                    menu+="null,'','','',''));";
                }

                
foreach(DataRow drsecond in second)
                
{    
                    menu
+="msub"+two+"=new mMenuItem("+"'"+drfirst["text"]+"','',"+"'"+drfirst["target"]+"'";
                    menu
+=","+drfirst["visible"]+",";//是否可见,false可见,true不可见
                    menu+="'','1','','','','');";

                    menu
+="msub"+three+".addsubItem(new mMenuItem("+"'"+drsecond["text"]+"'";//description
                    menu+=","+"'"+drsecond["url"]+"'"+",";//url
                    menu+="'"+drsecond["target"]+"'"+",";//target
                    menu+=""+drsecond["visible"]+",";//是否可见,false可见,true不可见
                    menu+="'"+drsecond["status"]+"'"+",";//状态条
                    menu+="null,'','','',''));";
                    menu
+="mpmenu"+one+".addItem(msub"+two+");";//addItem
                    three++;
                }

                two
++;
            }

            one
++;
        }

        
return menu;
    }

    
#endregion

}

3.数据库脚本
 

-- 建表
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[topmenu] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ topmenu ]
GO

CREATE   TABLE   [ dbo ] . [ topmenu ]  (
    
[ ID ]   [ int ]   NOT   NULL  ,
    
[ parentId ]   [ int ]   NOT   NULL  ,
    
[ text ]   [ nvarchar ]  ( 255 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ url ]   [ nvarchar ]  ( 255 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ target ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ IsBoot ]   [ int ]   NULL  ,
    
[ visible ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  ,
    
[ status ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL  
ON   [ PRIMARY ]
GO

-- 插入测试数据
insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 1 , 0 , ' 深圳 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 0 , ' false ' , ' 深圳 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 2 , 1 , ' 南山 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 1 , ' false ' , ' 南山 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 3 , 1 , ' 福田 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 1 , ' false ' , ' 福田 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 4 , 2 , ' 科技园 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 2 , ' false ' , ' 科技园 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 5 , 3 , ' 塞格 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 2 , ' false ' , ' 塞格 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 6 , 0 , ' 广州 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 0 , ' false ' , ' 广州 ' )

insert   into  topmenu( [ ID ] , [ parentId ] , [ text ] , [ url ] , [ target ] , [ IsBoot ] , [ visible ]  , [ status ] )
values ( 7 , 6 , ' 广州火车站 ' , ' http://singlepine.cnblogs.com/articles/259955.html ' , ' self ' , 1 , ' false ' , ' 广州火车站 ' )
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值