+1-888-365-2779
Try Now
More in this section

Forums / Designing with Sitefinity / This really helped me, I hope it helps someone else.

This really helped me, I hope it helps someone else.

7 posts, 0 answered
  1. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    05 Dec 2008
    Link to this post
    I have spent the last 2 days or so trying to get the Sitefinity Breadcrumb to render as an unordered list rather than a text string.  I tried deriving from the breadcrumb control and overriding methods, but I could not even determine which methods to override.  The Render() method for a composite control is a little strange.

    Then I stumbled across CSS Adapters which Sitefinity already makes use of for the hyperlink and radio button control.  Here is the CSS Adapter which makes the Sitefinity Breadcrumb control render as an unordered list.  It supports the RenderCurrentNodeAsLink and CSSSelectorClass properties, but I had trouble getting the ShowStartingNode property working, but as that wasn't relevant to what I was doing I stopped there.

    Anyways, here's the code, hope it helps someone else.
    using System;  
    using System.IO;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.HtmlControls;  
     
    namespace CSSFriendly  
    {  
     
        public class BreadCrumbAsList : System.Web.UI.WebControls.Adapters.WebControlAdapter  
        {  
     
            public BreadCrumbAsList()  
            {  
                //  
            }  
     
            protected override void OnInit(EventArgs e)  
            {  
                base.OnInit(e);  
            }  
     
            protected override void RenderBeginTag(HtmlTextWriter writer)  
            {  
                if (Control != null)  
                {  
                    writer.WriteLine();  
                    writer.WriteBeginTag("ul");  
                    if ((Control.Attributes["CssSelectorClass"] != null) && (Control.Attributes["CssSelectorClass"].Length > 0))  
                    {  
                        writer.WriteAttribute("class", Control.Attributes["CssSelectorClass"] + " AspNet-siteMap");  
                    }  
                    else  
                    {  
                        writer.WriteAttribute("class", "AspNet-siteMap");  
                    }  
                    writer.Write(HtmlTextWriter.TagRightChar);  
                    writer.Indent++;  
                }  
                writer.WriteLine();  
                writer.WriteBeginTag("li");  
                writer.Write(HtmlTextWriter.TagRightChar);  
            }  
     
            protected override void RenderEndTag(HtmlTextWriter writer)  
            {  
                writer.WriteEndTag("li");  
                writer.Indent--;  
                writer.WriteLine();  
                writer.WriteEndTag("ul");  
                writer.WriteLine();  
            }  
     
            protected override void RenderContents(HtmlTextWriter writer)  
            {  
                Telerik.Cms.Web.UI.Breadcrumb bc = (Telerik.Cms.Web.UI.Breadcrumb)Control;  
                SiteMapPath item = (SiteMapPath)Control;  
                SiteMapProvider Provider = ((System.Web.UI.WebControls.SiteMapPath)(Control)).Provider;  
                SiteMapNodeCollection collection = new SiteMapNodeCollection();  
                SiteMapNode node = Provider.CurrentNode;  
                if (node != null)  
                {  
                    collection.Add(node);  
                    while (node != Provider.CurrentNode.RootNode)  
                    {  
                        nodenode = node.ParentNode;  
                        collection.Add(node);  
                    }  
                }  
                BuildItems(collection, true, writer);  
                writer.WriteLine();  
            }  
     
     
            private void BuildItems(SiteMapNodeCollection items, bool isRoot, HtmlTextWriter writer)  
            {  
                Telerik.Cms.Web.UI.Breadcrumb bc = (Telerik.Cms.Web.UI.Breadcrumb)Control;  
                if (items.Count > 0)  
                {  
                    writer.WriteLine();  
                    writer.Indent++;  
                    for (int i = items.Count - 1; i > -1; i--)  
                    {  
                        // Write href links for item, if item is not currentNode  
                          
                        if (i != 0 || (i==0 && bc.RenderCurrentNodeAsLink))  
                        {  
                            BuildItem(items[i], writer, i);  
                        }  
     
                        else  
                        {  
                            // if node is currentNode just print the title  
                            writer.WriteLine();  
                            writer.Indent++;  
                            writer.Write(items[i].Title);  
                            writer.Indent--;  
                        }  
                    }  
                    writer.Indent--;  
                    writer.WriteLine();  
                }  
            }  
     
            private void BuildItem(SiteMapNode item, HtmlTextWriter writer, Int32 itemnumber)  
            {  
                Telerik.Cms.Web.UI.Breadcrumb bc = (Telerik.Cms.Web.UI.Breadcrumb)Control;  
                if ((item != null) && (writer != null))  
                {  
                    if (item.Url.Length > 0)  
                    {  
                        writer.WriteLine();  
                        writer.WriteBeginTag("a");  
                        writer.WriteAttribute("href", Page.ResolveUrl(item.Url));  
                        writer.WriteAttribute("class", "AspNet-SiteMap-Link");  
                        writer.Write(HtmlTextWriter.TagRightChar);  
                        writer.Indent++;  
                        writer.WriteLine();  
                        writer.Write(item.Title);  
                        writer.Indent--;  
                        writer.WriteLine();  
                        writer.WriteEndTag("a");  
                        writer.WriteLine();  
                        if (itemnumber != 0)  
                        {  
                            writer.Indent--;  
                            writer.Write("</li><li>");  
                            writer.Indent++;  
                        }  
                        writer.WriteLine();  
                    }  
                }  
            }  
        }  

    Happy Coding!

    -Nathan
  2. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    05 Dec 2008
    Link to this post
    Credit where credit is due, I forgot to mention that I modified the code found in the following post http://forums.asp.net/p/1081411/1683273.aspx to build that Control Adapter.
  3. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    08 Dec 2008
    Link to this post
    Hello Nathan,

    Thanks a lot for sharing your code sample. As a note of gratitude your Telerik points have been updated.

    Kind regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Isaac
    Isaac avatar
    63 posts
    Registered:
    01 Dec 2008
    11 Dec 2008
    Link to this post
    There is a bug in the code is submitted unfortunately, please replace the RenderContents function from the above post with:

    protected override void RenderContents(HtmlTextWriter writer)

    {

    Telerik.Cms.Web.UI.Breadcrumb bc = (Telerik.Cms.Web.UI.Breadcrumb)Control;

    SiteMapPath item = (SiteMapPath)Control;

    SiteMapProvider Provider = ((System.Web.UI.WebControls.SiteMapPath)(Control)).Provider;

    SiteMapNodeCollection collection = new SiteMapNodeCollection();

    SiteMapNode node = Provider.CurrentNode;

    if (node != null)

    {

    collection.Add(node);

    while (node != Provider.CurrentNode.RootNode)

    {

    if (node.ParentNode != null)

    {

    node = node.ParentNode;

    if (node != null)

    {

    collection.Add(node);

    }

    }

    }

    }

    BuildItems(collection, true, writer);

    writer.WriteLine();

    }

  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    12 Dec 2008
    Link to this post
    Hi Nathan,

    Thank you for the follow up!

    Sincerely yours,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. marco pessina
    marco pessina avatar
    16 posts
    Registered:
    02 Nov 2009
    05 Aug 2010
    Link to this post
    Hi,
    I've changed this line in BuildItem sub to add the separator specified in the PathSeparator property:

    If itemnumber <> 0 Then
        writer.Indent -= 1
        writer.Write(String.Format("</li><li>{0}</li><li>", bc.PathSeparator))
        writer.Indent += 1
    End If

    Bye
  7. Juyong
    Juyong avatar
    1 posts
    Registered:
    13 Aug 2010
    13 Aug 2010
    Link to this post
    Hello Nathan,

    Your solution is exactly what I need for my case. However, I am ASP.NET beginner and new to Sitefinity, so I don't know what exactly to do to use your code. Could you explain how to make it work? Thank you.
Register for webinar
7 posts, 0 answered