RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Nine ASP.NET Site Navigation Problem Solutions: Part 1 : Page 4

Find out how to use ASP.NET 2.0's site navigation controls to not only make building site navigation displays simple, but also solve real-world problems, such as hiding selected pages, or displaying "breadcrumbs."

#6 Centering Yin & Yang by Centralizing Meta-Data
ASP.NET 2.0's ability to store arbitrary page information centrally can solve a great number of problems. For instance, consider the simplicity of maintaining meta-keywords and meta-descriptions in the Web.sitemap rather than distributed across every page. The new approach makes it simple to insert such metadata in the rendered HTML.

     <title><%# mstrTitle %></title>
     <meta name="description" content="<%# mstrMetaDescription %>" />
     <meta name="keywords" content="<%# mstrMetaKeywords %>" />
Then simply declare and set a couple of protected properties in the code-behind:

   protected string mstrMetaKeywords = "";
   protected string mstrMetaDescription = "";
   protected string mstrTitle = "";
   protected void Page_Load(object sender, EventArgs e) {
     if (SiteMap.CurrentNode != null) {
       if (SiteMap.CurrentNode["metaKeywords"] != null)
         mstrMetaKeywords = SiteMap.CurrentNode["metaKeywords"];
       mstrMetaDescription = SiteMap.CurrentNode.Description;
       mstrTitle = SiteMap.CurrentNode.Title;
     if (!Page.IsPostBack)
Don't forget to check whether the CurrentNode property or any custom-defined SiteMapNode attributes are null. Lastly add metaKeywords and description attributes to your Web.sitemap file:

Figure 3: Displaying TreeView Navigation: The Maintenance page in the solution accompanying this article contains a TreeView that links to a SiteMapDataSource and acts as a site map.
     title="Site Map" 
     metaKeywords="Site Map" 
     description="The Site Map provides links to every 
        page on the site organized in a logical hierarchy." />
This approach vastly simplifies maintaining page-level metadata used by internal or external search engines. By combining the technique with a Master Page, you may just write your Web administrator out of a job!

#7: Sitemap Simplicity
Adding a functional site map to your site is now as easy as adding a SiteMapDataSource and a TreeView to your Web form, and then setting the Treeview.DataSourceID property to the SiteMapDataSource. To hide the root node just set the SiteMapDataSource.ShowStartingNode Property to false. You can make the TreeView a little prettier by setting the ShowExpandCollapse property to false and ShowLines to true. See Figure 3.

Don't Miss the Next Installment!
This article demonstrated seven problems and their solutions using basic ASP.NET 2.0 site navigation techniques in the context of scenarios that developers are most likely to encounter. The second installment in this article series provides two more problems and solutions, and introduces more advanced techniques, including: hiding unauthorized pages, extending the site map provider model to include dynamic content, and caching dynamic content using ASP.NET 2.0's new SqlCacheDependency class.

Lee Richardson works as a senior software engineer for the Near Infinity consulting company, specializing in ASP.Net, SharePoint, and other rapid application development technologies. Lee has nine years of software development experience, is a Microsoft Certified Solution Developer (MCSD), and a Project Management Professional (PMP). For related topics see Lee's blog.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date