October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Set Your Web Site's Navigation Structure Easily with ASP.NET 2.0 Site Maps

  • May 24, 2006
  • By Bipin Joshi
  • Send Email »
  • More Articles »

Using the SiteMap Data Source Control

The use of site maps is not limited just to the SiteMapPath control. You also can attach the site map to navigational controls such as TreeView. In the following example, you will use the same site map file to bind with a TreeView.

Add a new Web form called SiteMapDataSourceDemo.aspx to the Web site. Drag and drop a SiteMap data source control (SiteMapDataSource1) and a TreeView (TreeView1) to the form. Set the DataSourceID property of the TreeView to SiteMapDataSource1. Also, set the ShowLines property of the TreeView control to true. The following is the complete markup of SiteMapDataSourceDemo.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
         CodeFile="SiteMapDataSourceDemo.aspx.cs"
         Inherits="SiteMapDataSourceDemo" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
      <asp:TreeView ID="TreeView1" runat="server"
                    DataSourceID="SiteMapDataSource1"
                    ShowLines="True">
      </asp:TreeView>
      <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
   </form>
</body>
</html>

Now, run the Web form and observe how the same navigation structure is rendered automatically in the TreeView (see Figure 8).

Figure 8: Binding Site Map File to a TreeView

Using SiteMap Class

Displaying site map data in the SiteMapPath or TreeView control works great. However, sometimes you may need to devise custom rendering logic. For example, you may want to develop a custom navigation control that displays only the parent levels vertically. In such cases, you need to access the site map file programmatically. The SiteMap class allows you to do exactly that.

The SiteMap path has two important properties: RootNode and CurrentNode. Both are of type SiteMapNode, and both give you the reference of the root node and current node of the site map, respectively. Table 7 lists some of the important properties of the SiteMapNode class.

Property Description
ChildNodes Represents the collection of all the child nodes of the current node
HasChildNodes Indicates whether the site map node has child nodes (true/false)
Title Returns the value of the title attribute as specified in the site map file
Url Returns the value of the url attribute as specified in the site map file
Description Returns the value of the description attribute as specified in the site map file
ParentNode Gives reference of the parent site map node of the current node

Table 7: Important Properties of the SiteMapNode Class

The following example uses the SiteMap path to access the individual nodes of a site map file. Then, it programmatically adds them to a TreeView control.

Add a new Web form called SiteMapCustom.aspx. Drag and drop a TreeView control on it. Add the following code to the Page_Load event of the Web form:

protected void Page_Load(object sender, EventArgs e)
{
   int count = SiteMap.RootNode.ChildNodes.Count;
   for (int i = 0; i < count; i++)
   {
      SiteMapNode smNode=SiteMap.RootNode.ChildNodes[i];
      TreeNode tvNode = new TreeNode(smNode.Title, "", "",
                                     smNode.Url, "");
      TreeView1.Nodes.Add(tvNode);
      if (smNode.HasChildNodes)
      {
         int childCount=smNode.ChildNodes.Count;
         for (int j = 0; j < childCount; j++)
         {
            SiteMapNode smChildNode = smNode.ChildNodes[j];
            TreeNode tvChildNode = new TreeNode(smChildNode.Title,
                                                "", "",
                                                smChildNode.Url, "");
            tvNode.ChildNodes.Add(tvChildNode);
         }
      }
   }
}

Here, you first get the total number of child nodes in the root node. Then, you loop through the ChildNodes collection of the root node. With each iteration, you create a new instance of the TreeNode class and specify its title and url in its constructor. You then add this TreeNode to the Nodes collection of the TreeView. Next, you check if the current SiteMapNode has any child nodes. If it does, you iterate through them, repeating the TreeNode-creation process. Note that this time you add the new TreeNodes to the ChildNodes collection of the current TreeNode object.

Note that because you knew that there were only two levels of nesting, you used two for loops. To make your logic more generic, you may use recursion to populate the TreeView.

Run the Web form and you should again see something similar to Figure 8.

Using Security Trimming

Web sites often implement role-based security schemes. For example, you may have different roles in your application such as Administrators, ProductTesters, and ServiceTesters. In such cases, you frequently need to control the site-navigation links shown to the users. For example, if the currently logged-in user belongs to the ProductTesters role, you may want the links related to products to be displayed and any others to be hidden. One way to deal with roles this way is via manual coding, but it involves programmatically taking care of all the authorization logic. Fortunately, the site map file and SiteMap data source control together provide a feature called security trimming to help you.

To test security trimming, you need to enable membership and roles features on your Web site. Open the web.config file and add the following markup in it:

<authentication mode="Forms" />
<authorization>
   <deny users="?"></deny>
</authorization>

Here, you've set the authentication mode to Forms. You also set the authorization rule such that anonymous users are denied access to the Web site. Next, you need to enable the role-management feature by adding the following tag to the web.config file:

<roleManager enabled="true" />

Next, select WebSite > ASP.NET Configuration from the VS.NET menu bar to open the Web Site Administration Tool. Use the tool to add two roles: ProductTesters and ServiceTesters (see Figure 9). When a user belonging to the ProductTesters role signs in, you want only the product-related links to display in the navigation TreeView. Similarly, when a user belonging to the ServiceTesters role signs in, only the service-related links should display in the TreeView.



Click here for a larger image.

Figure 9: Using the Web Site Administration Tool to Add Roles

Use the Web Site Administration Tool to create two users called user1 and user2. Add user1 to ProductTesters role and user2 to ServiceTesters role (see Figure 10).



Click here for a larger image.

Figure 10: Creating Users Using Web Site Administration Tool

Note: By default, user and role information is stored in the ASPNETDB database from the App_Data folder of your Web site. This database is created automatically by ASP.NET if it doesn't exist already.

Now, add a new site map file named SecurityTrimming.sitemap to the Web site and key in the following markup:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="default.aspx" title="Home" description="My Web Site">
<siteMapNode title="Products" roles="ProductTesters">
<siteMapNode url="~/products/product1.aspx" title="First Product" />
<siteMapNode url="~/products/product2.aspx" title="Second Product" />
</siteMapNode>
<siteMapNode title="Services" roles="ServiceTesters">
<siteMapNode url="~/services/service1.aspx" title="First Service" />
<siteMapNode url="~/services/service2.aspx" title="Second Service" />
</siteMapNode>
<siteMapNode url="contact.aspx" title="Contact Us" />
</siteMapNode>
</siteMap>

Most of this markup is the same as that in the web.sitemap file. However, there is one important attribute added to the Products and Services siteMapNodes: roles. The roles attribute specifies the role to which this node and its child nodes are accessible. Because product-related links are to be displayed only to the users belonging to ProductTesters, you set the roles attribute of the Products siteMapNode to ProductTesters. Along the same lines, you set the roles attribute of Services siteMapNode to ServiceTesters. The siteMapNodes that do not have the roles attribute specified are accessible to all users. Also, note that the Products and Services nodes no longer have the URL attribute specified.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel