In My Web.sitemap I have the following:

<siteMapNode url="~/Groups/ViewGroups.aspx" urlRoute="groups/{PostId}/{PostTitle}" />
</siteMapNode>

In my MasterPage I have implemented the ItemDataBound event to try and set the title of each page that implements the master page dynamically but for some reason the title is not being set.

protected void SiteMapPath1_ItemDataBound(object sender, SiteMapNodeItemEventArgs e)
{
    string CurrentNodeTitle = GetTitleFromDatabase();

    if (e.Item.ItemType == SiteMapNodeItemType.Current) {
         e.Item.SiteMapNode.Title = CurrentNodeTitle;
    }
}

If I set the title in the Web.sitemap then it works perfectly but when I set it using e.Item.SiteMapNode.Title = CurrentNodeTitle; the title is nto being set.

Dani AI

Generated

Note for : the behaviour you're seeing is common — changing the SiteMapNode.Title on the sitemap object doesn't reliably update what's already been rendered by the SiteMapPath control (and changing the provider's node can be unsafe because those node instances are shared across requests). Two safer, practical options follow.

Change the breadcrumb control text directly in ItemDataBound (this updates what the user sees immediately) and also set the page <title> explicitly so the browser title is correct:

protected void SiteMapPath1_ItemDataBound(object sender, SiteMapNodeItemEventArgs e)
{
    string dynamicTitle = GetTitleFromDatabase(); // your lookup

    if (e.Item.ItemType == SiteMapNodeItemType.Current)
    {
        // Replace the rendered control text rather than mutating the underlying SiteMapNode
        foreach (Control ctrl in e.Item.Controls)
        {
            var hl = ctrl as System.Web.UI.WebControls.HyperLink;
            if (hl != null) { hl.Text = dynamicTitle; break; }

            var lbl = ctrl as System.Web.UI.WebControls.Label;
            if (lbl != null) { lbl.Text = dynamicTitle; break; }

            var lit = ctrl as System.Web.UI.LiteralControl;
            if (lit != null) { lit.Text = System.Web.HttpUtility.HtmlEncode(dynamicTitle); break; }
        }

        // Keep the browser title in sync
        Page.Title = dynamicTitle;
    }
}

If you need many sitemap entries to show DB-driven titles across pages, implement a custom SiteMapProvider (or generate sitemap nodes dynamically) so the provider itself supplies the correct Title values. Quick troubleshooting: verify the ItemDataBound handler is wired, put a breakpoint/log inside it, and confirm GetTitleFromDatabase() returns the expected value (and encoding) before assigning to the control.

So there is no solution to this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.