-------------------------------------
源代码下载/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"];
GetDataSet#region GetDataSet
public static DataSet GetDataSet(string sql)
{
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
Web Form Designer generated code#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
LoadTopMenu#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','广州火车站')