代码:
1<?xml version="1.0" encoding="utf-8" ?>
2<CataLog>
3 <Links id="1" Text="产品库1" roles="123">
4 <Link id="2" Text="产品库11" roles="123"></Link>
5 <Link id="3" Text="产品库12" roles="113"></Link>
6 <Links id="11" Text="产品库1" roles="123">
7 <Link id="24" Text="产品库www" roles="1213"></Link>
8 <Link id="34" Text="产品库333" roles="113"></Link>
9 <Links id="17" Text="产品库dd1" roles="123">
10 <Link id="244" Text="产品库sds" roles="123"></Link>
11 <Link id="344" Text="产品库433" roles="113"></Link>
12 </Links>
13 </Links>
14 </Links>
15 <Links id="9" Text="产品库2" roles="123">
16 <Link id="5" Text="产品库21" roles="123"></Link>
17 <Link id="6" Text="产品库22" roles="123"></Link>
18 </Links>
19 <Link id="8" Text="产品库3" roles="123"></Link>
20</CataLog>
2<CataLog>
3 <Links id="1" Text="产品库1" roles="123">
4 <Link id="2" Text="产品库11" roles="123"></Link>
5 <Link id="3" Text="产品库12" roles="113"></Link>
6 <Links id="11" Text="产品库1" roles="123">
7 <Link id="24" Text="产品库www" roles="1213"></Link>
8 <Link id="34" Text="产品库333" roles="113"></Link>
9 <Links id="17" Text="产品库dd1" roles="123">
10 <Link id="244" Text="产品库sds" roles="123"></Link>
11 <Link id="344" Text="产品库433" roles="113"></Link>
12 </Links>
13 </Links>
14 </Links>
15 <Links id="9" Text="产品库2" roles="123">
16 <Link id="5" Text="产品库21" roles="123"></Link>
17 <Link id="6" Text="产品库22" roles="123"></Link>
18 </Links>
19 <Link id="8" Text="产品库3" roles="123"></Link>
20</CataLog>
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
3<html xmlns="http://www.w3.org/1999/xhtml" >
4<head runat="server">
5 <title>无标题页</title>
6</head>
7<body>
8 <form id="form1" runat="server">
9 <asp:Menu runat="server" ID="Menu1" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" StaticSubMenuIndent="10px">
10 <Items>
11
12 </Items>
13 <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
14 <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
15 <DynamicMenuStyle BackColor="#FFFBD6" />
16 <StaticSelectedStyle BackColor="#FFCC66" />
17 <DynamicSelectedStyle BackColor="#FFCC66" />
18 <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
19 <StaticHoverStyle BackColor="#990000" ForeColor="White" />
20 </asp:Menu>
21 </form>
22</body>
23</html>
2
3<html xmlns="http://www.w3.org/1999/xhtml" >
4<head runat="server">
5 <title>无标题页</title>
6</head>
7<body>
8 <form id="form1" runat="server">
9 <asp:Menu runat="server" ID="Menu1" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" StaticSubMenuIndent="10px">
10 <Items>
11
12 </Items>
13 <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
14 <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
15 <DynamicMenuStyle BackColor="#FFFBD6" />
16 <StaticSelectedStyle BackColor="#FFCC66" />
17 <DynamicSelectedStyle BackColor="#FFCC66" />
18 <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
19 <StaticHoverStyle BackColor="#990000" ForeColor="White" />
20 </asp:Menu>
21 </form>
22</body>
23</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadMenu();
}
}
private void loadMenu()
{
XmlDocument dom = new XmlDocument();
dom.Load(MenuPath.XmlPath);
XmlElement root = dom.DocumentElement;
if (root.ChildNodes.Count == 0)
throw new Exception("Data Source is null");
XmlNodeList nodelist = root.ChildNodes;
initMenu(nodelist);
}
public void initMenu(XmlNodeList nodeList)
{
MenuItem item = null;
for (int i= 0; i < nodeList.Count; i++)
{
if (!isInRole(nodeList[i].Attributes["roles"].Value))
continue;
item = new MenuItem();
item.Text = nodeList[i].Attributes["Text"].Value;
item.Value = nodeList[i].Attributes["id"].Value;
if (nodeList[i].Name == "Links")
GetChildNodeData(nodeList[i], item);
Menu1.Items.Add(item);
}
}
bool isInRole(string rolesid)
{
bool b = false;
string[] rd = rolesid.Trim().Trim(',').Split(',');
foreach (string s in rd)
{
if (s == "123")
{
b = true;
break;
}
}
return b;
}
private void GetChildNodeData(XmlNode node,MenuItem pItem)
{
MenuItem item = null;
foreach (XmlNode n in node.ChildNodes)
{
if (!isInRole(n.Attributes["roles"].Value))
continue;
item = new MenuItem();
item.Text = n.Attributes["Text"].Value;
item.Value = n.Attributes["id"].Value;
pItem.ChildItems.Add(item);
if (n.Name == "Links")
GetChildNodeData(n, item);
}
}
{
if (!IsPostBack)
{
loadMenu();
}
}
private void loadMenu()
{
XmlDocument dom = new XmlDocument();
dom.Load(MenuPath.XmlPath);
XmlElement root = dom.DocumentElement;
if (root.ChildNodes.Count == 0)
throw new Exception("Data Source is null");
XmlNodeList nodelist = root.ChildNodes;
initMenu(nodelist);
}
public void initMenu(XmlNodeList nodeList)
{
MenuItem item = null;
for (int i= 0; i < nodeList.Count; i++)
{
if (!isInRole(nodeList[i].Attributes["roles"].Value))
continue;
item = new MenuItem();
item.Text = nodeList[i].Attributes["Text"].Value;
item.Value = nodeList[i].Attributes["id"].Value;
if (nodeList[i].Name == "Links")
GetChildNodeData(nodeList[i], item);
Menu1.Items.Add(item);
}
}
bool isInRole(string rolesid)
{
bool b = false;
string[] rd = rolesid.Trim().Trim(',').Split(',');
foreach (string s in rd)
{
if (s == "123")
{
b = true;
break;
}
}
return b;
}
private void GetChildNodeData(XmlNode node,MenuItem pItem)
{
MenuItem item = null;
foreach (XmlNode n in node.ChildNodes)
{
if (!isInRole(n.Attributes["roles"].Value))
continue;
item = new MenuItem();
item.Text = n.Attributes["Text"].Value;
item.Value = n.Attributes["id"].Value;
pItem.ChildItems.Add(item);
if (n.Name == "Links")
GetChildNodeData(n, item);
}
}