<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/" title="Home" description="Home">
<siteMapNode url="" description="Menu 1" title="Menu 1">
<siteMapNode url="" description="SubMenu 1" title="Sub Menu 1"/>
</siteMapNode>
<siteMapNode url="" description="Menu 2" title="Menu 2"/>
<siteMapNode url="" description="Menu 3" title="Menu 3"/>
</siteMapNode>
</siteMap>
Sample code to add to the page menu:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal"
DataSourceID="SiteMapDataSource1">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
Running the application, we will have something like the next picture:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/" title="Home" description="Home" visible="true">
<siteMapNode url="" description="Menu 1" title="Menu 1" visible="true">
<siteMapNode url="" description="SubMenu 1" title="Sub Menu 1" visible="true"/>
</siteMapNode>
<siteMapNode url="" description="Menu 2" title="Menu 2" visible="true"/>
<siteMapNode url="" description="Menu 3" title="Menu 3" visible="true"/>
</siteMapNode>
</siteMap>
The attribute "visible" is that we will indicate whether or not the menu is shown, and we'll add the event MenuItemDataBound on Menu with the following code:
protected void NavigationMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{ SiteMapNode node = e.Item.DataItem as SiteMapNode;
if (!string.IsNullOrEmpty(node["visible"]))
{
bool isVisible;
if (bool.TryParse(node["visible"], out isVisible))
{
if (!isVisible)
{
if (e.Item.Parent != null)
e.Item.Parent.ChildItems.Remove(e.Item);
else
((Menu)sender).Items.Remove(e.Item);
}
}
}
}
Thus, we have our menu to show all nodes whose attribute value equals True.
<asp:TreeView runat="server" ID="tvMenus" AutoGenerateDataBindings="False" DataSourceID="XmlDsSiteMap"
OnTreeNodeCheckChanged="tvMenus_TreeNodeCheckChanged" ShowCheckBoxes="All" ShowLines="True"
OnTreeNodeDataBound="tvMenus_TreeNodeDataBound">
<DataBindings>
<asp:TreeNodeBinding DataMember="siteMapNode" SelectAction="None" ShowCheckBox="True"
TextField="title" />
<asp:TreeNodeBinding DataMember="siteMapNode" TextField="title" />
<asp:TreeNodeBinding DataMember="siteMapNode" TextField="title" />
<asp:TreeNodeBinding DataMember="siteMap" />
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="XmlDsSiteMap" runat="server" DataFile="~/Web.sitemap" XPath="/*/*/*">
</asp:XmlDataSource>
protected void tvMenus_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{ XmlElement node = e.Node.DataItem as XmlElement;
if (node.Attributes["visible"] != null)
{ if (!string.IsNullOrEmpty(node.Attributes["visible"].Value))
{
bool isVisible;
if (bool.TryParse(node.Attributes["visible"].Value, out isVisible))
{
e.Node.Checked = isVisible;
}
else
e.Node.Checked = true;
}
else
e.Node.Checked = true;
}
}
menu2f.png
protected void tvMenus_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{ XmlDsSiteMap.GetXmlDocument().SelectSingleNode(e.Node.DataPath)
.Attributes["visible"].Value = e.Node.Checked.ToString();
}
And we add the button to save the changes the following code:
protected void btn_Click(object sender, EventArgs e)
{ XmlDsSiteMap.Save();
}
Now just enable and disable these items like we want to.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)