views:

891

answers:

2

Ok, so I'm building bread crumbs and depending on the value of the breadcrumb an image will be the seperator. So "HOME" will have one image and "SEARCH" will have another.

I know I can do this programatically (at least I ASSUME) but is there an easier way to do this? Can I link an image to a node based on the value of the node? Can I do it with PathSeparatorTemplate?

Thank you.

+2  A: 

You can put an

<asp:Image ... />

into the PathSerparatorTemplate but you still have to set the image url from code.

Aleris
and I would do that by iterating through the nodes, and testing its value... then how would I reference the image in the template? would it be templatename.imagecontrolid?Thank you for your help.
Sara Chipps
The contents of a template are instantiated many times, so the image with the same id would exists for each node of the path. In order to have access at each such image you can use the OnItemDataBound event and use e.Item.FindControl("ID_OF_IMAGE") to get the image.
Aleris
thanks, you're awesome.
Sara Chipps
A: 

I see you have already accepted an answer, but I thought some code would help, so here is some:

Site1.Master


    <asp:SiteMapPath ID="SiteMapPath1" Runat="server" OnItemDataBound="Item_Bound">
        <PathSeparatorTemplate>
            <asp:Image ID="SepImage" runat="server" ImageUrl="/images"/>
        </PathSeparatorTemplate>
    </asp:SiteMapPath>

Site1.Master.cs


    private string lastItemKey = "";
    public void Item_Bound(Object sender, SiteMapNodeItemEventArgs e)
    {
        if (e.Item.ItemType == SiteMapNodeItemType.PathSeparator)
        {
            string imageUrl = ((Image) e.Item.Controls[1]).ImageUrl;
            imageUrl += lastItemKey + ".png";
            ((Image) e.Item.Controls[1]).ImageUrl = imageUrl;
        }
        else
        {
            lastItemKey = e.Item.SiteMapNode.Key;
        }
    }

Then I have an /images directory containing an image for each of the Key's of the SiteMapNodes. In other terms: this code will result in the image being displayed, after each of the path nodes, to depend on the key of the node before it.

Hope this helps someone.

Alfred B. Thordarson