后台
程序代码如下:
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 webapplication6
{
/// <summary>
/// webform1 的摘要说明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected coalesys.webmenu.webmenu csnetmenu;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
csnetmenu.menubar.absolutedockenabled = false;
csnetmenu.menubar.absolutedragenabled = false;
csnetmenu.menubar.backgroundcolor = "";
csnetmenu.menubar.outerhighlightcolor = "#666666";
csnetmenu.menubar.outershadowcolor = "#666666";
csnetmenu.menubar.innershadowcolor = "#f9f8f7";
csnetmenu.menubar.hovercolor = "#dfdfdf";
csnetmenu.menubar.selectedcolor = "#b6bdd2";
csnetmenu.menubar.selectedtextcolor = "#000000";
csnetmenu.backgroundcolor = "";
csnetmenu.selectedcolor = "#b6bdd2";
csnetmenu.outerhighlightcolor = "#c0c0c0";
csnetmenu.outershadowcolor = "#c0c0c0";
csnetmenu.innershadowcolor = "#808080";
csnetmenu.popupicon = "./images/arrow-black.gif";
csnetmenu.selectedpopupicon = "./images/arrow-white.gif";
csnetmenu.clearpixelimage = "./images/clearpixel.gif";
// populate webmenu
loadwebmenudata(csnetmenu);
}
//=============================================================================
// loadwebmenudata - load webmenu from database
//
// input:
// cswebmenu - [in] coalesys.webmenu.webmenu object
//
// output:
// none
//
public void loadwebmenudata(coalesys.webmenu.webmenu cswebmenu)
{
coalesys.webmenu.group csmenugroup;
// database info
string dbconnstring = "provider=microsoft.jet.oledb.4.0;data source=";
string dbpathstring = server.mappath("./selfreferencedtable.mdb");
string dbsqlstring = "select * from nodes order by id";
// initiate oledb interface
oledbconnection dbconn = new oledbconnection(dbconnstring + dbpathstring);
oledbcommand dbcomm = new oledbcommand(dbsqlstring, dbconn);
oledbdataadapter dbadapter = new oledbdataadapter();
dbconn.open();
// fill an ado.net dataset
dataset ds = new dataset();
dbadapter.selectcommand = dbcomm;
dbadapter.fill(ds, "menuitems");
dbconn.close();
// create the data relation between the id and parent_id columns of the menuitems table.
// (this is the key to hierarchical navigating in a self-referencing table).
datarelation dr = ds.relations.add("menuitemhierarchy",
ds.tables["menuitems"].columns["id"],
ds.tables["menuitems"].columns["parent_id"]);
// start top-down navigation of the menuitem rows.
foreach(datarow dbmenuitem in ds.tables["menuitems"].rows)
{
// if the parent_id colum is null, then this is a root menu item.
if(dbmenuitem.isnull("parent_id"))
{
// create a menu group for the root menu item
csmenugroup = cswebmenu.groups.add();
csmenugroup.caption = dbmenuitem["caption"].tostring();
// execute the recursive function to populate all it's children.
addmenuitems(dbmenuitem.getchildrows(dr), dr, csmenugroup);
}
}
}
//=============================================================================
// addmenuitems - recursive function to populate hierarchical menu items
// from data rows that have parent/child relationships.
//
// input:
// datarows - [in] child rows
// datarel - [in] data relation
// webmenugroup - [in] webmenu group
//
// output:
// none
//
public void addmenuitems(datarow[] datarows, datarelation datarel, coalesys.webmenu.group webmenugroup)
{
coalesys.webmenu.item csmenuitem;
coalesys.webmenu.group csnestedmenugroup;
datarow[] drchildren;
foreach(datarow dbmenuitem in datarows)
{
csmenuitem = webmenugroup.items.add();
csmenuitem.caption = dbmenuitem["caption"].tostring();
csmenuitem.url = dbmenuitem["url"].tostring();
if (dbmenuitem["enable"].tostring()=="true" )
{
csmenuitem.enabled=true;
}
else
{
csmenuitem.enabled=false;
}
// check if this item has children of it's own
drchildren = dbmenuitem.getchildrows(datarel);
// if so, create a group for the children and reenter this function.
if(drchildren.length > 0)
{
csnestedmenugroup = csmenuitem.addgroup();
addmenuitems(drchildren, datarel, csnestedmenugroup);
}
}
}
}
效果图如下:

其中可以看到,help菜单有两项内容:contents和about,其中,contents又有topics和objectreference,但是这里的topics已经被disenable了,不能点击,菜单的使能由数据库里的enable字段决定,这样实现了动态菜单的生成。如果菜单的项目由改变,只需要在数据库里面进行操作就可以了,从而实现了web页面实现菜单的基本功能。除了能够实现这些功能外,还可以在菜单中添加图标,可停靠等。如下图:

索要源代码和作者联系方式:l_yx123@sina.com.cn,谢谢支持。