我希望有一个简单的菜单,每个菜单都有一个子菜单,我想要自己的风格,不想在visual studio中使用菜单控件,任何人都可以建议如何拥有这样的东西? 并且重要的注意事项是:我想要一个动态菜单我的意思是我想从数据库中读取菜单项和子菜单项。
示例:
menu-1
sub-menu-1
sub-menu-2
menu-2
sub-menu-1
sub-menu-2
可能是具有这种结构的表格:
menuid
menutitle
menuhref null able
parrentid null able
我知道我可以使用XML和ASP菜单控件,但我不想使用XML,因为你知道ASP菜单控件生成一些我不想要它们的额外html代码,我想要自己的风格,ul ,li和类或元素的id,我有自己的风格和结构,只想知道如何获取菜单项和子菜单并列出它们。 谢谢你的帮助。
答案 0 :(得分:1)
我想我可以这样做,谢谢Rashed: 查询创建表和一些测试数据:
CREATE TABLE [dbo].[Menus](
[MenuID] [bigint] NOT NULL,
[ParentID] [bigint] NULL,
[Name] [nvarchar](50) NULL,
[URL] [nvarchar](50) NULL,
CONSTRAINT[PK_Menus] PRIMARY KEYCLUSTERED
(
[MenuID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (1, 0, N’Main Menu1′, N’Main Menu1′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (2, 0, N’Main Menu2′, N’Main Menu2′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (3, 0, N’Main Menu3′, N’Main Menu3′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (4, 1, N’Sub Menu 1.1′, N’Sub Menu 1.1′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (5, 1, N’Sub Menu 1.2′, N’Sub Menu 1.2′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (6, 2, N’Sub Menu 2.1′, N’Sub Menu 2.1′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (7, 2, N’Sub Menu 2.2′, N’Sub Menu 2.2′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (8, 3, N’Sub Menu 3.1′, N’Sub Menu 3.1′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (9, 3, N’Sub Menu 3.2′, N’Sub Menu 3.2′)
INSERT [dbo].[Menus]([MenuID], [ParentID], [Name], [URL]) VALUES (10, 3, N’Sub Menu 3.2′, N’Sub Menu 3.3′)
并在页面加载中:
protected void Page_Load(object sender, EventArgse)
{
if (!IsPostBack)
{
DataTable oDataTable = newDataTable();
oDataTable = SelectDataTable(“SELECT MenuID, ParentID, Name, URL FROM Menus”);
DataRow[] drParentMenu = oDataTable.Select(“ParentID = 0″);
var oStringBuilder = newStringBuilder();
string MenuList = GenerateMenu(drParentMenu, oDataTable, oStringBuilder);
Literal1.Text = MenuList;
}
}
GetConnectionStrings
public string GetConnectionStrings()
{
stringConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
returnConStr;
}
SelectDataTable
public DataTable SelectDataTable(String Sql)
{
DataTable dt = new DataTable();
SqlConnection oSqlConnection = new SqlConnection(GetConnectionStrings());
try
{
oSqlConnection.Open();
SqlDataAdapter sqlda = new SqlDataAdapter(Sql, GetConnectionStrings());
sqlda.Fill(dt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
oSqlConnection.Close();
oSqlConnection.Dispose();
}
return dt;
}
GenerateMenu
private string GenerateMenu(DataRow[] drParentMenu, DataTable oDataTable, StringBuilder oStringBuilder)
{
oStringBuilder.AppendLine(“<ul>”);if (drParentMenu.Length > 0)
{
foreach (DataRow dr in drParentMenu)
{
string MenuURL = dr["URL"].ToString();
string MenuName = dr["Name"].ToString();
string line = String.Format(@”<li ><a href=”"{0}”">{1}</a>”, MenuURL, MenuName);
oStringBuilder.Append(line);
string MenuID = dr["MenuID"].ToString();
string ParentID = dr["ParentID"].ToString();
DataRow[] subMenu = oDataTable.Select(String.Format(“ParentID = {0}”, MenuID));
if (subMenu.Length > 0 && !MenuID.Equals(ParentID))
{
var subMenuBuilder = new StringBuilder();
oStringBuilder.Append(GenerateMenu(subMenu, oDataTable, subMenuBuilder));
}
oStringBuilder.Append(“</li>”);
}
}
oStringBuilder.Append(“</ul>”);
return oStringBuilder.ToString();
}