Question

I want my ASP.NET site to have simple menu string aka Breadcrumbs. I have created Sitemap with all required elements and registered into Web.config. For example:

<siteMap>
    <siteMapNode url="Default.aspx" title="Home" >
        <siteMapNode url="hosting/Default.aspx" title="Hosting" />
        <siteMapNode url="software/Default.aspx" title="Software">
            <siteMapNode url="firefox/Default.aspx" title="Firefox">
                <siteMapNode url="Download.aspx" title="Download" />
                <siteMapNode url="Support.aspx" title="Support" />
            </siteMapNode>
        </siteMapNode>
    </siteMapNode>
</siteMap>

And created a control placed on Masterpage. Here it's menu generation code:

protected void Control_Load(Object sender, EventArgs e)
{
    string path = String.Empty;
    StringCollection list = new StringCollection();

    foreach (string str in Request.Url.Segments)
    {
        path += str;
        string link = String.Format("<a href=\"{0}://{1}{2}\">{3}</a>", Request.Url.Scheme, Request.Url.Authority, path, this.names[str]);
        list.Add(link);
    }

    foreach (string str in list)
    {
        menu += String.Concat(str, SeparatorLine);
    }
    menu = menu.Remove(menu.LastIndexOf(SeparatorLine));
}

But it uses a StringDictionary like { { "/", "Home" }, { "hosting/", "Hosting" }, { "software/", "Software" } .. }

How can I use a query to Sitemap instead of it? Or maybe something else, not Sitemap, but beforehand invented.

Was it helpful?

Solution

ASP.NET SiteMapPath Control

<asp:SiteMapPath ID="SiteMapPath1" Runat="server" />

OTHER TIPS

You can use the SiteMapPath control (should be in the Navigation category of your toolbox). Check this page for a tutorial.

Just set the SiteMapProvider property to the provider for the sitemap you want to use for the breadcrumbs and you'll be set. I usually just place the SiteMapPath inside a div and set the CSS on the div to style the breadcrumbs.

One gotcha to look out for though. If you suppress any root nodes in the sitemap, they will still show up in your breadcrumbs. I've run into this by trying to use the same sitemap for breadcrumbs and for the SiteMapDataSource for a treeview where I wanted to not show the starting node.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top