Geeks With Blogs

@TiagoSalgado
  • TiagoSalgado Call blocking on #ios7 ... hum... PT you will be the first! about 530 days ago

News
Tiago Salgado .NET / SQL Server / IT / etc...

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.

Example of web.sitemap file:

<?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:

To show or hide the menu depending on the type of access each user, we may define the Roles in each 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.

For this, the web.sitemap will be something like:

<?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.

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.

<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;
     } }

Finally, to record the attribute changes depending on the state of the checkbox, we added to the Treeview TreeNodeCheckChanged event the following code:

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.

Posted on Thursday, April 14, 2011 1:55 PM | Back to top


Comments on this post: Control Visible Menus with web.sitemap

# dream vacation network
Requesting Gravatar...
i think you have a great site here... today was my first time coming here..
dream vacation network
dream vacation network
Left by Erik on Aug 25, 2011 10:35 AM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well
custom wine label
Left by alfred on Sep 04, 2011 12:45 PM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
never tried to program this way. I liked it appended the task. very interesting. thesis online thank for this code sharing.
Left by jackcollins on Sep 05, 2011 3:27 PM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
I agree with you. This type of projects should be encouraged and I think that these type of projects are the projects for the future. . . .
Left by cool garage on Dec 02, 2011 1:49 PM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
I've got to style my personal thanks for the generosity meant for individuals who need assistance on this essential subject material.
Left by Dried Fruit on Dec 21, 2011 9:27 AM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
I remember when I remember how hard it was to use asp.net to launch my first portal about mobile apps, I was very dissapointed about it.
Left by 250888enko on Feb 15, 2012 7:24 PM

# re: Control Visible Menus with web.sitemap
Requesting Gravatar...
This codes, helped me a lot... thanks... you did a great man...
Left by ipad case on Feb 25, 2012 1:23 PM

Your comment:
 (will show your gravatar)


Copyright © Tiago Salgado | Powered by: GeeksWithBlogs.net | Join free