Wednesday 25 December 2013

Adding Sitemap Path to the web application in ASP.NET

In this article we are going to see how to add the sitemap path in the web application, what is sitemap path ?

SiteMapPath displays the navigation path that shows the users about current page location and as well as the path of the site how to came to visit the current page. It is easy for user to move to the parent page.

Drag and Drop the sitemappath in the Master page and add a web.sitemap file where you can mention the datas about the map path and the display information for that url.

SiteMapFile:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode title="Root">
      <siteMapNode url="~/Homes.aspx" title="Home" />
      <siteMapNode url="~/Employee.aspx" title="Employee">
        <siteMapNode url="~/AddEmployee.aspx"   title="Add Employee"></siteMapNode>
      </siteMapNode>
      <siteMapNode url="~/Adm.aspx" title="Admin">
        <siteMapNode url="~/AddUser.aspx" title="Add User"/>
      </siteMapNode>
    </siteMapNode>
</siteMap>



HTML:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SiteMapPath ID="SiteMapPath1" runat="server"
            onitemcreated="SiteMapPath1_ItemCreated">
        </asp:SiteMapPath>
    <br />
    <div style="padding:10px">
   
    <asp:Menu DataSourceID="XmlDataSource1"  StaticMenuItemStyle-ItemSpacing="30px" DisappearAfter="2000"   runat="server" Orientation="Horizontal">
        <DataBindings>
         <asp:MenuItemBinding DataMember="Menu" TextField="value" NavigateUrlField="url" />
        </DataBindings>
    </asp:Menu>
      
        <asp:XmlDataSource ID="XmlDataSource1" DataFile="~/Menus.xml" XPath="Menus/Menu" runat="server"></asp:XmlDataSource>

    </div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1"  runat="server">                       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

C#
To avoid the display of root element from the sitemap and the hide the path separater for the index 1, find the below code

  protected void SiteMapPath1_ItemCreated(object sender, SiteMapNodeItemEventArgs e)
        {
            if (e.Item.ItemType == SiteMapNodeItemType.Root ||
                (e.Item.ItemType == SiteMapNodeItemType.PathSeparator && e.Item.ItemIndex==1))
            {
                e.Item.Visible = false;
            }
        }



Output:
You can see at the top of the application a path is shown how user is visit the current page.

Visiting Home Page: home.aspx

\

Visiting Add Employee Page: addemployee.aspx



Visiting Add User Page: adduser.aspx




From this article we can learn what is the usage of sitemappath and there implementation in the web application.

No comments:

Post a Comment