Displaying an automatically generated Site Map. : Site Maps « Development « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » Development » Site Maps 
9. 40. 12. Displaying an automatically generated Site Map.
File: App_Code\SqlSiteMapProvider.cs

using System;
using System.Collections.Specialized;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Caching;

namespace MyNamespace
{
    public class SqlSiteMapProvider : StaticSiteMapProvider
    {
        private bool _isInitialized = false;
        private string _connectionString;
        private SiteMapNode _rootNode;

        public override void Initialize(string name, NameValueCollection attributes)
        {
            if (_isInitialized)                return;

            base.Initialize(name, attributes);

            string connectionStringName = attributes["connectionStringName"];
            if (String.IsNullOrEmpty(connectionStringName))
                throw new Exception("You must provide a connectionStringName attribute");

            _connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
            if (String.IsNullOrEmpty(_connectionString))
                throw new Exception("Could not find connection string " + connectionStringName);

            _isInitialized = true;
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return BuildSiteMap();
        }

        public override SiteMapNode BuildSiteMap()
        {
            lock (this)
            {
                HttpContext context = HttpContext.Current;
                _rootNode = (SiteMapNode)context.Cache["RootNode"];

                if (_rootNode == null)
                {
                    HttpContext.Current.Trace.Warn("Loading from database");

                    Clear();

                    DataTable tblSiteMap = GetSiteMapFromDB();

                    _rootNode = GetRootNode(tblSiteMap);                    
                    AddNode(_rootNode);

                    BuildSiteMapRecurse(tblSiteMap, _rootNode);

                    SqlCacheDependency sqlDepend = new SqlCacheDependency("SiteMapDB""SiteMap");
                    context.Cache.Insert("RootNode", _rootNode, sqlDepend);
                }
                return _rootNode;

            }
        }


        private DataTable GetSiteMapFromDB()
        {
            string selectCommand = "SELECT Id,ParentId,Url,Title,Description FROM SiteMap";
            SqlDataAdapter dad = new SqlDataAdapter(selectCommand, _connectionString);
            DataTable tblSiteMap = new DataTable();
            dad.Fill(tblSiteMap);
            return tblSiteMap;
        }


        private SiteMapNode GetRootNode(DataTable siteMapTable)
        {
            DataRow[] results = siteMapTable.Select("ParentId IS NULL");
            if (results.Length == 0)
                throw new Exception("No root node in database");
            DataRow rootRow = results[0];
            return new SiteMapNode(this, rootRow["Id"].ToString(), rootRow["url"].ToString(), rootRow["title"].ToString(), rootRow["description"].ToString());
        }

        private void BuildSiteMapRecurse(DataTable siteMapTable, SiteMapNode parentNode)
        {
            DataRow[] results = siteMapTable.Select("ParentId=" + parentNode.Key);
            foreach (DataRow row in results)
            {
                SiteMapNode node = new SiteMapNode(this, row["Id"].ToString(), row["url"].ToString(), row["title"].ToString(), row["description"].ToString());
                AddNode(node, parentNode);
                BuildSiteMapRecurse(siteMapTable, node);
            }
        }

    }
}

            
File: Web.Config

<configuration>
  <connectionStrings>
    <add
      name="conSiteMap"
      connectionString="Data Source=.\SQLExpress;Integrated
Security=True;AttachDbFileName=|DataDirectory|SiteMapDB.mdf;User Instance=True"/>
</connectionStrings>

  <system.web>
    <siteMap defaultProvider="myProvider">
      <providers>
        <add
          name="myProvider"
          type="MyNamespace.SqlSiteMapProvider"
          connectionStringName="conSiteMap" />

      </providers>
    </siteMap>

      <caching>
      <sqlCacheDependency enabled = "true" pollTime = "5000" >
        <databases>
          <add name="SiteMapDB"
               connectionStringName="conSiteMap"
          />
        </databases>
      </sqlCacheDependency>
      </caching>

      </system.web>
</configuration>
9. 40. Site Maps
9. 40. 1. Defining a Site Map
9. 40. 2. The SiteMapPath control enables you to navigate to any parent page of the current page.
9. 40. 3. Format SiteMapPath
9. 40. 4. Using a template with the SiteMapPath control.
9. 40. 5. Using the SiteMap Class
9. 40. 6. Adding nodes to a Site Map dynamically.
9. 40. 7. Using the SiteMapNode Class
9. 40. 8. To display different links to different users depending on their roles, enable the Security Trimming
9. 40. 9. Merging Multiple Site Maps
9. 40. 10. Creating Custom Site Map Attributes
9. 40. 11. Creating the AutoSiteMapProvider
9. 40. 12. Displaying an automatically generated Site Map.
9. 40. 13. Removing the root node from the retrieved node collection
9. 40. 14. using the StartFromCurrentNode property
9. 40. 15. Using the StartingNodeUrl property
9. 40. 16. Working with the CurrentNode object (C#)
9. 40. 17. Working with the CurrentNode object (VB)
9. 40. 18. Creating a custom navigation display using the CurrentNode property (C#)
9. 40. 19. Creating a custom navigation display using the CurrentNode property (VB)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.