Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 158 - Binding asp.net menu control to web.sitemap file using sitemapdatasource control

Suggested Videos
Part 155 - Binding menu control to xml file
Part 156 - Binding asp.net menu control to database table
Part 157 - SiteMapPath control in asp.net



This is continuation to Part 157. Please watch Part 157, before proceeding. In Part 155, we discussed binding menu control to an xml file using xmldatasource control.

It is also possible to bind menu control to web.sitemap file.



Here are the steps to achieve this.
1. Add web.sitemap file. Copy and paste the following XML
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
  <siteMapNode title="DummyRoot">
    <siteMapNode url="~/Home.aspx" title="Home"/>
    <siteMapNode url="~/Employee.aspx" title="Employee">
      <siteMapNode url="~/UploadResume.aspx" title="Upload Resume"/>
      <siteMapNode url="~/EditResume.aspx" title="Edit Resume"/>
      <siteMapNode url="~/ViewResume.aspx" title="View Resume"/>
    </siteMapNode>
    <siteMapNode url="~/Employer.aspx" title="Employer">
      <siteMapNode url="~/UploadJob.aspx" title="Upload Job"/>
      <siteMapNode url="~/EditJob.aspx" title="Edit Job"/>
      <siteMapNode url="~/ViewJob.aspx" title="View Job"/>
    </siteMapNode>
    <siteMapNode url="~/Admin.aspx" title="Admin">
      <siteMapNode url="~/AddUser.aspx" title="Add User"/>
      <siteMapNode url="~/EditUser.aspx" title="Edit User"/>
      <siteMapNode url="~/ViewUser.aspx" title="View User"/>
    </siteMapNode>
  </siteMapNode>
</siteMap>

2. Drag and drop "SiteMapDataSource" control onto the webform. By default, a SiteMapDataSource control reads data automatically from web.sitemap file. 
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

3. Finally drag and drop Menu control onto the webform and set DataSourceID attribute, to the ID of the SiteMapDataSource control created in Step 2.
<asp:Menu ID="Menu1" DataSourceID="SiteMapDataSource1" runat="server">
</asp:Menu>

That's it. Run the application and notice that, the Menu control is bound to web.sitemap file. One problem here is that, the Root node is also displayed.

Hiding Root node in the Menu control.
To hide the Root node in the Menu control, set ShowStartingNode attribute of the SiteMapDataSource control to false.
<asp:SiteMapDataSource ShowStartingNode="false" ID="SiteMapDataSource1" runat="server" />

No comments:

Post a Comment

It would be great if you can help share these free resources