Search code examples
asp.netlinq-to-sqldynamic-databreadcrumbsasp.net-dynamic-data

How to create breadcrumbs using Dynamic Data/LINQTOSQL framework?


I need some EXAMPLES or IDEAS on how to created dynamic breadcrumb control which will dynamically generated breadcrumbs for Dynamic Date framework powered by LINQTOSQL


Solution

  • You'll probably need three things:

    1. A hierarchy structure in your database*
    2. A custom SiteMap provider based on the StaticSiteMapProvider to read the hierarchy
    3. A SiteMapPath control to display the Breadcrumb.

    * I'd done some hunting around when I wanted something similar for a client site, and decided that storing the path structure in the database would be simplest - I've given the answer previously here for an arbitrary depth site map - note that if you're using SQL2008 you can use the new HierarchyId data type to make this a bit easier.

    That being said, if you've got things like categories and products, you can probably get away with a simpler system in your database.

    The key functions I needed to create to resolve this were things like:

    /// <summary>
    /// Gets this SiteMaps children.
    /// </summary>
    /// <value>The children.</value>
    public List<SiteMap> Children {
      get {
        if (null == m_Children && !m_AttemptedToLoadChildren) {
          m_AttemptedToLoadChildren = true;
    
          m_Children = ctx.GetSiteMapChildrenByPath(_Path, 1).ToList();
    
          // Sorts ascending.
          m_Children.Sort(( sm1, sm2 ) => sm1.SortOrder.CompareTo(sm2.SortOrder));
          // CMS Sorts Descending, so reverse the list.
          m_Children.Reverse();
        }
    
        return m_Children;
      }
    }
    
    /// <summary>
    /// Gets a value indicating whether this instance has any children.
    /// </summary>
    /// <value>
    ///  <c>true</c> if this instance has children; otherwise, <c>false</c>.
    /// </value>
    public bool HasChildren {
      get {
        if (null != Children && Children.Any()) {
          m_HasChildren = true;
        }
    
        return m_HasChildren;
      }
    }
    
    /// <summary>
    /// Gets this SiteMaps parent.
    /// </summary>
    /// <value>The parent.</value>
    public SiteMap Parent {
      get {
        if (null == m_Parent && null != _ParentId) {
          m_Parent = ctx.GetSiteMap(_ParentId);
        }
    
        return m_Parent;
      }
    }
    

    GetSiteMap and GetSiteMapChildrenByPath call into stored procs to build the hierarchy as pulling it out with LINQ was going to be quite complex.