ComponentArt. Menu
的用法
.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Web_Menu
{
public partial class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) buildMenu();
}
private void buildMenu()
{
OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("siteMap.mdb"));
dbCon.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM SiteMap ORDER BY NodeId", dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["NodeId"], ds.Tables[0].Columns["ParentNodeId"]);
foreach (DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow.IsNull("ParentNodeId"))
{
ComponentArt.Web.UI.MenuItem newItem = CreateItem(dbRow);
Menu1.Items.Add(newItem);
PopulateSubMenu(dbRow, newItem);
}
}
}
private void PopulateSubMenu(DataRow dbRow, ComponentArt.Web.UI.MenuItem item)
{
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
ComponentArt.Web.UI.MenuItem childItem = CreateItem(childRow);
item.Items.Add(childItem);
PopulateSubMenu(childRow, childItem);
}
}
private ComponentArt.Web.UI.MenuItem CreateItem(DataRow dbRow)
{
ComponentArt.Web.UI.MenuItem item = new ComponentArt.Web.UI.MenuItem();
item.Text = dbRow["Text"].ToString();
item.NavigateUrl = dbRow["NavigateUrl"].ToString();
item.LookId = dbRow["LookId"].ToString();
item.Look.LeftIconUrl = dbRow["LeftIcon"].ToString();
item.Look.HoverLeftIconUrl = dbRow["LeftIconHover"].ToString();
item.Look.RightIconUrl = dbRow["RightIcon"].ToString();
item.Look.HoverRightIconUrl = dbRow["RightIconHover"].ToString();
return item;
}
}
}
效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/a4023c4af597c51f033cd8fc998646f7.jpeg)