Freebies

SiteMapHyperLink

We use this one a lot and the funny thing is that it is really simple but it took us a while to think of doing it. So what is SiteMapHyperLink? Say you are using a SiteMap for your navigation for a website. Using the asp:menu the links are already done for you when the menu is built. But what if you have a random link in the middle of your page that you want to reference a sitemap node? Then you have two choices in that case: hardcore the link using the NavigateUrl attribute, or in the page load event set the NavigateUrl.

    public class SiteMapHyperLink : HyperLink
    {
        public string SiteMapNode
        {
            get { return NavigateUrl; }
            set
            {
                SiteMapNode node = SiteMap.Provider.FindSiteMapNodeFromKey(value.ToString());
                if (node !=null)
                {
                    NavigateUrl = node.Url;
                    if (!string.IsNullOrEmpty(node.Title) && string.IsNullOrEmpty(ToolTip))
                        ToolTip = node.Title;

                    if (!string.IsNullOrEmpty(node.Title) && string.IsNullOrEmpty(Text))
                        Text = node.Title;
                }
            }
        }
    }

That is it for the contrl.

How to Use

Simple Case

    <sn:SiteMapHyperLink SiteMapNode="Products" runat="server" Target="_blank" ID="_productsLink1" /> 
    Result Products

Custom Text Case

    <sn:SiteMapHyperLink Text="Our Great Items" SiteMapNode="Products" runat="server" Target="_blank" ID="_productsLink2" /> 
    Result Our Great Items

Image Case

    <sn:SiteMapHyperLink ID="_productsLink3" runat="server" SiteMapNode="Products" Target="_blank" Text="">
        <asp:Image runat="server" ID="_image" SkinID="SoftwareBoxImage"/>
    </sn:SiteMapHyperLink> 
    Result Sharp Notions Software in a Box