sitemap.json
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource.
一种使菜单在我们的网站上工作的快捷方法是使用Menu控件,然后使用SiteMapDataSource将其分配给web.sitemap。
Example of web.sitemap file:
web.sitemap文件的示例:
<?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>
<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" />
menu1rc.png
menu1rc.png
To show or hide the menu depending on the type of access each user, we may define the Roles in each SiteMapNode.
要根据每个用户的访问类型显示或隐藏菜单,我们可以在每个SiteMapNode中定义角色。
Another way to control the menus visible, is to add an attribute in each SiteMapNode and depending on its value, or will not display each menu.
控制菜单可见的另一种方法是在每个SiteMapNode中添加一个属性,具体取决于其值,否则将不显示每个菜单。
For this, the web.sitemap will be something like:
为此,web.sitemap将类似于:
<?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>
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);
}
}
}
}
To be able to directly control the menus that will be visible or not, I used a Treeview to bind the web.sitemap file and set all items to show a checkbox, which will indicate the status of the Visible attribute.
为了能够直接控制是否显示菜单,我使用树视图绑定了web.sitemap文件,并将所有项目设置为显示一个复选框,该复选框将指示Visible属性的状态。
<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
menu2f.png
Finally, to record the attribute changes depending on the state of the checkbox, we added to the Treeview TreeNodeCheckChanged event the following code:
最后,为了记录取决于复选框状态的属性更改,我们将以下代码添加到Treeview TreeNodeCheckChanged事件中:
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();
}
menu3x.png
menu3x.png
This is a very simple way to allow your application users control each Menu visibility and avoid to change the web.sitemap manually.
这是一种非常简单的方法,可让您的应用程序用户控制每个菜单的可见性,并避免手动更改web.sitemap。
You may have to make some changes in order to adjust to your requirements, but this is a good point.
您可能必须进行一些更改才能适应您的要求,但这是一个好主意。
翻译自: https://www.experts-exchange.com/articles/5261/Control-Visible-Menus-with-web-sitemap.html
sitemap.json