![HomePage.JPG](https://i-blog.csdnimg.cn/blog_migrate/4ea561d6f3e5c7a040e3bf4589ede284.jpeg)
-------------------------------------
源代码下载/Files/singlepine/topmenu.rar
public
class
TopMenu : System.Web.UI.Page
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected DataRow[] father;
protected DataRow[] first;
protected DataRow[] second;
private void Page_Load(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
GetDataSet#region GetDataSet
public static DataSet GetDataSet(string sql)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
LoadTopMenu#region LoadTopMenu
protected string LoadTopMenu()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
second=dsFather.Tables[0].Select("ParentID='"+Convert.ToInt32(drfirst["ID"])+"' and IsBoot=2","IsBoot");
if(second.Length==0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
menu+="msub"+two+"=new mMenuItem("+"'"+drfirst["text"]+"','',"+"'"+drfirst["target"]+"'";
menu+=","+drfirst["visible"]+",";//是否可见,false可见,true不可见
menu+="'','1','','','','');";
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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
![None.gif](/Images/OutliningIndicators/None.gif)
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
![None.gif](/Images/OutliningIndicators/None.gif)
--
插入测试数据
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
1
,
0
,
'
深圳
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
0
,
'
false
'
,
'
深圳
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
2
,
1
,
'
南山
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
1
,
'
false
'
,
'
南山
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
3
,
1
,
'
福田
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
1
,
'
false
'
,
'
福田
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
4
,
2
,
'
科技园
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
2
,
'
false
'
,
'
科技园
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
5
,
3
,
'
塞格
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
2
,
'
false
'
,
'
塞格
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
6
,
0
,
'
广州
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
0
,
'
false
'
,
'
广州
'
)
![None.gif](/Images/OutliningIndicators/None.gif)
insert
into
topmenu(
[
ID
]
,
[
parentId
]
,
[
text
]
,
[
url
]
,
[
target
]
,
[
IsBoot
]
,
[
visible
]
,
[
status
]
)
values
(
7
,
6
,
'
广州火车站
'
,
'
http://singlepine.cnblogs.com/articles/259955.html
'
,
'
self
'
,
1
,
'
false
'
,
'
广州火车站
'
)