Wednesday 25 December 2013

Binding Sitemap file to Menu Control in ASP.NET


In this article we are going to see how to bind a sitemap file to the menu control, For  that we have to create a site map file and map all the URL with display name to the web.sitemap file. Next drag and drop the SiteMapDataSource to the master page. Mention the ShowStartingNode as False in SiteMapDataSource. For xmldatasource we have to specify the databindings in menu control, but for the sitemapdatasource it is not needed.

web.sitemap:
<?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:

  <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="SiteMapDataSource1"  StaticMenuItemStyle-ItemSpacing="30px" DisappearAfter="2000"   runat="server" Orientation="Horizontal">
    </asp:Menu>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"  ShowStartingNode="false" />
    </div>

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


Output :






From this article you can learn how to bind the SiteMap file to the Menu control.


No comments:

Post a Comment