日期:2014-05-20  浏览次数:20964 次

如何实现的动态可拆装的网页菜单
我今天看了一个印刷厂的内部网站,这个网站的菜单是动态的,就是把菜单名和子菜单名都装在数据库里的一个表中,子菜单就代表着一个网页,如果要加入新的页面,只需要将这个页面的地址写进数据库的子菜单项就可以了,请问这如何实现的。

------解决方案--------------------
public class HierarchicalMenu : System.Web.UI.UserControl
{
private Cuyahoga.Web.UI.PageEngine _page;
protected System.Web.UI.WebControls.PlaceHolder plhNodes;

private void Page_Load(object sender, System.EventArgs e)
{
if (this.Page is PageEngine)
{
this._page = (PageEngine)this.Page;
BuildNavigationTree();
}
}

private void BuildNavigationTree()
{
HtmlGenericControl mainList = new HtmlGenericControl( "ul ");
mainList.Controls.Add(BuildListItemFromNode(this._page.RootNode));
foreach (Node node in this._page.RootNode.ChildNodes)
{
if (node.ShowInNavigation && node.ViewAllowed(this._page.CuyahogaUser))
{
HtmlControl listItem = BuildListItemFromNode(node);
if (node.Level <= this._page.ActiveNode.Level
&& node.Id == this._page.ActiveNode.Trail[node.Level]
&& node.ChildNodes.Count > 0)
{
listItem.Controls.Add(BuildListFromNodes(node.ChildNodes));
}
mainList.Controls.Add(listItem);
}
}
if (this._page.CuyahogaUser != null
&& this._page.CuyahogaUser.HasPermission(AccessLevel.Administrator))
{
HtmlGenericControl listItem = new HtmlGenericControl( "li ");
HyperLink hpl = new HyperLink();
hpl.NavigateUrl = this._page.ResolveUrl( "~/Admin ");
hpl.Text = "Admin ";
listItem.Controls.Add(hpl);
mainList.Controls.Add(listItem);
}
this.plhNodes.Controls.Add(mainList);
}

private HtmlControl BuildListItemFromNode(Node node)
{
HtmlGenericControl listItem = new HtmlGenericControl( "li ");
HyperLink hpl = new HyperLink();
hpl.NavigateUrl = UrlHelper.GetUrlFromNode(node);
UrlHelper.SetHyperLinkTarget(hpl, node);
hpl.Text = node.Title;
// Little dirty trick to highlight the active item :)
if (node.Id == this._page.ActiveNode.Id)
{
hpl.CssClass = "selected ";
}
listItem.Controls.Add(hpl);
return listItem;
}

private HtmlControl BuildListFromNodes(IList nodes)
{
HtmlGenericControl list = new HtmlGenericControl( "ul ");
foreach (Node node in nodes)
{
if (node.ViewAllowed(this._page.CuyahogaUser))
{
HtmlControl listItem = BuildListItemFromNode(node);
if (node.Level <= this._page.ActiveNode.Level
&& node.Id == this._page.ActiveNode.Trail[node.Level]
&& node.ChildNodes.Count > 0)
{
listItem.Controls.Add(BuildListFromNodes(node.ChildNodes));
}
list.Controls.Add(listItem);
}
}
return list;
}
一个CMS系统的动态菜单