Breadcrumb Navigation

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="path.ascx.cs" Inherits="Custom_UserControls_path"  %>
    <!-- <%=controlname %> Start -->
    <asp:Repeater ID="mpath" runat="server">
        <HeaderTemplate>
        <ul class="<%= _CssClass %>"></HeaderTemplate>
        <ItemTemplate>
            <%#GetListItem(Container.DataItem)%></ItemTemplate>
        <FooterTemplate>
        </ul></FooterTemplate>
    </asp:Repeater>
    <!-- <%=controlname %> End -->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Web.UI; public partial class Custom_UserControls_path : System.Web.UI.UserControl
{ #region Parameters
#region RenderCurrentNodeAsLink
public enum CurrentNodeAsLinkOptions
{
False,
True
}
public CurrentNodeAsLinkOptions _RenderCurrentNodeAsLink = CurrentNodeAsLinkOptions.False ;
public CurrentNodeAsLinkOptions RenderCurrentNodeAsLink
{
get { return _RenderCurrentNodeAsLink; }
set { _RenderCurrentNodeAsLink = value; }
}
#endregion
#region PathDirection
public enum PathDirectionOptions
{
CurrentToRoot,
RootToCurrent
}
public PathDirectionOptions _PathDirection = PathDirectionOptions.RootToCurrent ;
public PathDirectionOptions PathDirection
{
get { return _PathDirection; }
set { _PathDirection = value; }
}
#endregion
#region ShowRootNode
public enum ShowRootNodeOptions
{
True,
False
}
public ShowRootNodeOptions _ShowRootNode = ShowRootNodeOptions.True;
public ShowRootNodeOptions ShowRootNode
{
get { return _ShowRootNode; }
set { _ShowRootNode = value; }
}
#endregion
#region CssClass
public String _CssClass ="crumb";
public String CssClass
{
get { return _CssClass; }
set { _CssClass = value; }
}
#endregion
#region HomeNodeTitle
public String _HomeNodeTitle = "Home";
public String HomeNodeTitle
{
get { return _HomeNodeTitle; }
set { _HomeNodeTitle = value; }
}
#endregion
#region HomeNodeImage
public String _HomeNodeImage = "Home.png";
public String HomeNodeImage
{
get { return _HomeNodeImage; }
set { _HomeNodeImage = value; }
}
#endregion
#region HomeNodeUrl
public String _HomeNodeUrl = "";
public String HomeNodeUrl
{
get { return _HomeNodeUrl; }
set { _HomeNodeUrl = value; }
}
#endregion #region ControlName
public string controlname
{
get
{
string temp = GetType().ToString();
temp = temp.Replace("ASP.", "/");
temp = temp.Replace("_ascx", ".ascx");
temp = temp.Replace("_", "/");
return temp;
}
}
#endregion
#endregion
protected void Page_Load(object sender, EventArgs e)
{
mpath.DataSource = getnodes();
mpath.DataBind();
} private SiteMapNodeCollection getnodes()
{
SiteMapNodeCollection mlist = new SiteMapNodeCollection();
SiteMapNode node = SiteMap.CurrentNode;
SiteMapNode root = SiteMap.RootNode;
SiteMapNode mnode; while (node != null)
{
// if ((_ShowRootNode == ShowRootNodeOptions.False) & (node == root))
// break; mnode = node.Clone();
mnode.Url = ResolveUrl(mnode.Url); if (_PathDirection == PathDirectionOptions.RootToCurrent)
mlist.Insert(0, mnode);
else
mlist.Add(mnode); node = node.ParentNode;
}
return mlist;
} protected String GetListItem(object dataItem)
{
SiteMapNode node = dataItem as SiteMapNode;
HtmlAnchor a = new HtmlAnchor();
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlImage img = new HtmlImage(); if (node.Url == _HomeNodeUrl)
{
img.Src = "/images/home.png";
img.Alt = "Home";
img.Attributes.Add("class", "home");
img.Attributes.Add("width", "16px");
img.Attributes.Add("height", "16px"); a.HRef = "/home.aspx";
a.Controls.Add(img);
}
else
{
a.HRef = Page.ResolveUrl(node.Url);
a.InnerText = node.Title;
} if (node.Url == Page.ResolveUrl(SiteMap.CurrentNode.Url))
{
li.InnerText = node.Title;
}
else
{
li.Controls.Add(a);
} StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw); li.RenderControl(hw);
return sb.ToString();
}
}

Created: Monday 24th May 2010 9:36 AM
Last Modified: Sunday 11th July 2010 6:46 PM LinkedIn link: View Chris Mills' profile on LinkedIn
Valid XHTML 1.0! Valid CSS! Level A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0