

Navigator : Home > Tutorials > Website Navigation Tutorials > ...
Using TreeView for navigating with Master Pages in C#
This tutorial shows how to implement TreeView navigation system, which is very powerful yet easy to employ. C# version.
Especially for the larger sites, navigation can get a little messy at times. Fortunately, Visual Studio makes it easier for us to manage navigation and also construct a hierarchical navigation system that lets users transition freely between pages, especially when it comes to making changes to the web site.
Create an XML file containing the site hierarchy, page titles and URLs.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Web.sitemap:
<?xml version="1.0" encoding="utf-8" ?> <siteMap>
<siteMapNode title="Home" description="Home" url="~/Default.aspx" >
<siteMapNode title="Links" description="Links to Other Sites"
url="~/Links.aspx"> <siteMapNode title="Links 1"
description="Links to sites 1" url="~/Links1.aspx" /> <siteMapNode title="Links 2"
description="Links to sites 2" url="~/Links2.aspx" /> </siteMapNode> <siteMapNode title="Games" description="Games you can play"
url="~/Games.aspx"> <siteMapNode title="Game 1" description="Game number 1"
url="~/Game1.aspx" /> <siteMapNode title="Game 2" description="Game number 2"
url="~/Game2.aspx" /> <siteMapNode title="Game 3" description="Game number 3"
url="~/Game3.aspx" /> </siteMapNode> </siteMapNode> </siteMap> |
Add a SiteMapDataSource onto your Master Page. Default configuration is set to retrieve the data from Web.sitemap
Also add a TreeView control onto your Master Page, choosing the Data Source of the Site Map. To also add further navigation on each page, which displays where the current page is in the hierarcy, you can add a SiteMapPath control. Again, by default, the SiteMapPath uses the Web.sitemap file for its hierarchy data.
To add an expandable menu system onto the page also, add the Menu control and choose the Data Source.
The Master Page should look something like this:
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Navigation.master.cs" Inherits="Navigation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title> </head> <body>
<form id="form1" runat="server"> <div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<table style="width: 100%">
<tr>
<td style="width: 100px">
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"> </asp:TreeView> <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"> </asp:Menu> </td> <td style="width: 100px">
<asp:SiteMapPath ID="SiteMapPath1" runat="server"> </asp:SiteMapPath> <br /> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"> </asp:ContentPlaceHolder> </td> </tr> </table> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html> |
All content pages should have the ContentPlaceHolders for the Master Page, and look something like this:
<%@ Page Language="C#" MasterPageFile="~/Navigation.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="Untitled Page" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>Home</h1> </asp:Content> |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!