asp.net 用XML生成放便擴(kuò)展的自定義樹(shù)
廢話(huà)不多說(shuō),直接上代碼(因一些原因,把部分?jǐn)?shù)據(jù)修改或精簡(jiǎn)了)。
XML代碼:
<?xml version="1.0" encoding="utf-8" ?>
<MenuData>
<Module Id="Business" Permissions="49,53,58,59,65,99,100,70,69" Name="業(yè)務(wù)管理" IsExpand="true">
<Menu Id="Orders" Permissions="49,53,58" Name="我的訂單" IsExpand="true">
<Item Permissions="49" IsOnlyVip="true" Name="預(yù)訂單" Url="Business/ProList.aspx" Title="預(yù)訂單"/>
<Item Id="Renting" Permissions="53" IsOnlyVip="true" Name="在訂單" Url="Business/List.aspx" Title="在訂單"/>
<Item Permissions="58" IsOnlyVip="true" Name="訂單歷史" Url="Business/RList.aspx" Title="訂單歷史"/>
</Menu>
<Menu Id="Clients" Permissions="59" Name="我的客戶(hù)" IsExpand="true">
<Item Id="ClientList" Permissions="59" Name="客戶(hù)列表" Url="Client/List.aspx" Title="客戶(hù)列表"/>
</Menu>
<Menu Id="" Permissions="65,99,100" Name="我的報(bào)表" IsExpand="true">
<Item Permissions="65" IsOnlyVip="true" Name="收入統(tǒng)計(jì)" Url="Report/Finance.aspx" Title="收入統(tǒng)計(jì)"/>
<Item Permissions="99" IsOnlyVip="true" Name="產(chǎn)品統(tǒng)計(jì)" Url="Report/Product.aspx" Title="產(chǎn)品統(tǒng)計(jì)"/>
<Item Permissions="100" IsOnlyVip="true" Name="客戶(hù)統(tǒng)計(jì)" Url="Report/Client.aspx" Title="客戶(hù)統(tǒng)計(jì)"/>
</Menu>
<Menu Id="" Permissions="70,69" Name="消息管理" IsExpand="true">
<Item Permissions="70" Name="發(fā)送站內(nèi)消息" Url="" Title="發(fā)送站內(nèi)消息" EventScript="onclick='OpenMsgWindow()'" />
<Item Id="ReceiveMsgList" Permissions="69" Name="消息接收列表" Url="Message/RList.aspx" Title="消息接收列表"/>
<Item Permissions="69" Name="消息發(fā)送列表" Url="Message/SList.aspx" Title="消息發(fā)送列表"/>
</Menu>
</Module>
<Module Id="Advertise" Permissions="89,90" Name="我要推廣" EventScript="OpenTuiGuangIndex();" >
<Menu Id="" Permissions="89" Name="產(chǎn)品推廣" Url="TuiGuang/Product.aspx" Title="產(chǎn)品推廣"/>
<Menu Id="" Permissions="90" Name="廣告推廣" Url="TuiGuang/Ad.aspx" Title="廣告推廣"/>
</Module>
<Module Id="SystemManage" Permissions="67,68" Name="系統(tǒng)設(shè)置">
<Menu Id="" Permissions="67" Name="更改系統(tǒng)密碼" Url="SysManage/Password.aspx" Title="更改系統(tǒng)密碼"/>
<Menu Id="" Permissions="68" Name="系統(tǒng)配置" Url="SysManage/Config.aspx" Title="系統(tǒng)配置"/>
</Module>
<Module Id="VIP" Permissions="" Name="會(huì)員介紹" IsUnVip="true">
<Menu Id="" Name="會(huì)員介紹" Url="VIP/index.html" Title="會(huì)員服務(wù)"/>
</Module>
</MenuData>
程序代碼:
using System.Text;
using System.Collections;
using System.Xml;
using System.Web;
using System;
/// <summary>
/// CreateTree 的摘要說(shuō)明
/// </summary>
public class MenuTree
{
int index = 0;//菜單欄目ID索引
private ArrayList havePermission = new ArrayList();
private bool isVip = false;
/// <summary>
/// 登錄用戶(hù)所擁有的權(quán)限
/// </summary>
private ArrayList HavePermissions
{
get { return havePermission; }
set { havePermission = value; }
}
/// <summary>
/// 登錄用戶(hù)是否是VIP
/// </summary>
private bool IsVip
{
get { return isVip; }
set { isVip = value; }
}
/// <summary>
/// 登錄用戶(hù)所擁有的權(quán)限 是否為VIP用戶(hù)
/// </summary>
/// <param name="havePermission"></param>
/// <param name="isVip"></param>
public MenuTree(ArrayList havePermission, bool isVip)
{
this.HavePermissions = havePermission;
this.IsVip = isVip;
}
/// <summary>
/// 綁定樹(shù)
/// </summary>
public string BindDataToTree()
{
System.Xml.XmlDocument document = new System.Xml.XmlDataDocument();
document.Load(HttpContext.Current.Server.MapPath("MenuData.xml"));
return CreateTreeHtml(document.DocumentElement, 0);
}
/// <summary>
/// 創(chuàng)建欄目樹(shù)
/// </summary>
/// <param name="document">xml節(jié)點(diǎn)</param>
/// <param name="deep">樹(shù)深度</param>
private string CreateTreeHtml(System.Xml.XmlNode document, int deep)
{
string nodeType = "Menu";//節(jié)點(diǎn)的類(lèi)型,來(lái)生成子節(jié)點(diǎn)的CSS類(lèi)型
StringBuilder treeHtml = new StringBuilder();
foreach (System.Xml.XmlNode node in document.ChildNodes)
{
string menuId = string.Empty;
string treeNodeHtml = string.Empty;
string nodeName = node.Name;
string showName = GetAttributesValue(node.Attributes["Name"]);//顯示欄目名
string nodeId = GetAttributesValue(node.Attributes["Id"]);//欄目ID
bool isExpand = GetAttributesValue(node.Attributes["IsExpand"]).ToLower().Trim() == "true" ? true : false;//是否展開(kāi)
string permissions = GetAttributesValue(node.Attributes["Permissions"]);//權(quán)限字串
bool isOnlyVip = GetAttributesValue(node.Attributes["IsOnlyVip"]).ToLower().Trim() == "true" ? true : false;//是否只允許VIP訪(fǎng)問(wèn)
bool isUnVip = GetAttributesValue(node.Attributes["IsUnVip"]).ToLower().Trim() == "true" ? true : false;//是否只準(zhǔn)非VIP訪(fǎng)問(wèn)
string eventScript = GetAttributesValue(node.Attributes["EventScript"]);//事件腳本
int chlidNodesCount = node.ChildNodes.Count;//子節(jié)點(diǎn)數(shù)
bool isPermissions = GetIsPermissions(permissions);//是否有權(quán)限
if (!isPermissions)
{
continue; //如果沒(méi)有權(quán)限,不生成此節(jié)點(diǎn)
}
if (nodeName == "Module")
{
if (isUnVip && IsVip)
{
continue;//如果為VIP會(huì)員 設(shè)為不允許訪(fǎng)問(wèn)子欄目
}
menuId = GetMenuId(nodeId);
treeHtml.AppendFormat("<div class='Module' id='Menu{0}' onclick='DoNodes(this);{1}' onselectstart='return false;'>", menuId, eventScript);
treeHtml.Append(" <img src='/images/sideMenuIcon.gif' alt='' /> ");
treeHtml.AppendFormat("<span>{0}</span>", showName);
treeHtml.Append("</div>");
deep = 0;
nodeType = "Module";
}
else
{
treeHtml.Append("<table cellpadding='0' cellspacing='0' style='border-width: 0;width:90%'>");
treeHtml.Append("<tr class='NodeLine'>");
for (int i = 0; i < deep; i++)
{
if (i == deep - 1)
{
treeHtml.Append("<td class='nodeIcoBox'>");
if (chlidNodesCount > 0)
{
menuId = GetMenuId(nodeId);
treeHtml.AppendFormat("<a id='Menu{0}' href='javascript:;' onclick='DoNodes(this,\"menu\")'><img src='/Images/{1}.gif' alt=''/></a>", menuId, (isExpand ? "open-menu" : "close-menu"));
}
else
{
treeHtml.Append("<img src='/Images/open-menuno.gif' alt=''/>");
}
treeHtml.Append("</td>");
}
else
{
treeHtml.Append("<td style='width: 20px;'> </td>");
}
}
string url = GetAttributesValue(node.Attributes["Url"]); //鏈接地址
string title = GetAttributesValue(node.Attributes["Title"]);//鏈接TITLE信息
string menuNodeId = nodeId.Trim().Length > 0 ? "id='MenuNode" + nodeId + "'" : string.Empty;//樹(shù)節(jié)點(diǎn)ID
treeHtml.Append("<td style='white-space: nowrap;'>");
if (url.Length > 0 || chlidNodesCount == 0)
{
if (!isOnlyVip || (isOnlyVip && IsVip))//欄目是否只為VIP開(kāi)放
{
if (url.Length > 0)
{
treeHtml.AppendFormat("<a href='{0}' target='MainFrame' title='{1}' {3} {4}>{2}</a>", url, title, showName, eventScript, menuNodeId);
}
else
{
treeHtml.AppendFormat("<a href='javascript:;' target='MainFrame' title='{0}' {2} {3}>{1}</a>", title, showName, eventScript, menuNodeId);
}
}
else
{
treeHtml.AppendFormat("<a href='javascript:;' target='MainFrame' title='{1}' onclick='return AlertVip();' class='disableColor' {3}>{2}</a>", url, title, showName, menuNodeId);
}
}
else
{
treeHtml.AppendFormat("<a href='javascript:;' onclick='DoAClick(\"Menu{0}\");' title='{1}' {3} {4}>{2}</a>", menuId, title, showName, eventScript, menuNodeId);
}
treeHtml.Append("</td>");
treeHtml.Append("</tr>");
treeHtml.Append("</table>");
}
if (chlidNodesCount > 0)
{
treeNodeHtml = CreateTreeHtml(node, deep + 1);
}
if (treeNodeHtml.Length > 0)
{
treeHtml.AppendFormat("<div id='Menu{0}Nodes' {1} style='{2}'>", menuId, (nodeType == "Module" ? "class='Menus'" : "class='MenuNodes'"), (isExpand ? "display:block;" : "display: none;"));
treeHtml.Append(treeNodeHtml);
treeHtml.Append("</div>");
}
}
return treeHtml.ToString();
}
/// <summary>
/// 取得欄目的ID
/// </summary>
private string GetMenuId(string nodeId)
{
return nodeId.Length > 0 ? nodeId : Convert.ToString(++index);
}
/// <summary>
/// 取得節(jié)點(diǎn)值
/// </summary>
private string GetAttributesValue(XmlAttribute attributeValue)
{
return attributeValue == null ? "" : attributeValue.Value.Trim();
}
/// <summary>
/// 是否有權(quán)限
/// </summary>
private bool GetIsPermissions(string permissions)
{
if (HavePermissions.Count == 0)
{
return false;
}
if (permissions.Length == 0)
{
return true;
}
else
{
string[] arrPermissions = permissions.Split(',');
for (int i = 0; i < arrPermissions.Length; i++)
{
if (HavePermissions.Contains(arrPermissions[i].Trim()))
{
return true;
}
}
return false;
}
}
}
相關(guān)JS代碼:
function DoNodes(obj,type)
{
var divId=obj.id+'Nodes';
var qdivObj=$("#"+divId);
if(qdivObj.is(":hidden"))
{
qdivObj.show();
if(type=='menu')
{
$(obj).children("img").attr("src","Images/open-menu.gif");
}
else
{
qdivObj.siblings(".Menus:visible").hide();
}
}
else
{
qdivObj.hide();
if(type=='menu')
{
$(obj).children("img").attr("src","Images/close-menu.gif");
}
}
}
function DoAClick(id)
{
$("#"+id).click();
}
function AlertVip()
{
alert("非常抱歉,此模塊只對(duì)VIP會(huì)員開(kāi)放!");return false;
}
最后效果

此代碼我覺(jué)得易放便擴(kuò)展,只要稍改動(dòng)一下代碼就能使用在自己的項(xiàng)目中。
相關(guān)文章
ASP.NET通過(guò)第三方網(wǎng)站Bitly實(shí)現(xiàn)短鏈接地址程序
這篇文章主要介紹了ASP.NET通過(guò)第三方網(wǎng)站Bitly實(shí)現(xiàn)短鏈接地址程序的步驟,需要的朋友可以參考下。2016-06-06asp.net 字符串、二進(jìn)制、編碼數(shù)組轉(zhuǎn)換函數(shù)
字符串和二進(jìn)制數(shù)組轉(zhuǎn)換、將HTML文件顯示為頁(yè)面的一部分、UTF8和GB2312之間的轉(zhuǎn)換2010-01-01ASP.NET Core實(shí)現(xiàn)中間件的幾種方式
這篇文章介紹了ASP.NET Core實(shí)現(xiàn)中間件的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08通過(guò)HttpClient 調(diào)用ASP.NET Web API示例
本篇文章主要介紹了通過(guò)HttpClient 調(diào)用ASP.NET Web API示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Prism區(qū)域管理器IRegionManager用法介紹
這篇文章介紹了Prism區(qū)域管理器IRegionManager用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02一個(gè)事半功倍的c#方法 動(dòng)態(tài)注冊(cè)按鈕事件
前幾天在網(wǎng)上看見(jiàn)一個(gè)制作計(jì)算器的c#程序,其中有一個(gè)動(dòng)態(tài)注冊(cè)按鈕事件,覺(jué)的很有用。于是實(shí)際操作了一哈, 確實(shí)比較好。2010-04-04MAUI中實(shí)現(xiàn)構(gòu)建跨平臺(tái)原生控件
這篇文章介紹了MAUI中實(shí)現(xiàn)構(gòu)建跨平臺(tái)原生控件的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02asp.net 簡(jiǎn)便無(wú)刷新文件上傳系統(tǒng)
之前寫(xiě)過(guò)一個(gè)仿163網(wǎng)盤(pán)無(wú)刷新多文件上傳系統(tǒng),已經(jīng)對(duì)無(wú)刷新上傳文件的原理做了詳細(xì)的分析而這次的系統(tǒng)主要是針對(duì)單個(gè)file控件的,便攜版,使用更簡(jiǎn)單,還有更深入的分析2012-05-05