Hi,
I've been trying to find an easy way of highlighting the current selected menu item of an asp.net menu (so the user knows which page they are on), but no matter what I have tried I can't get it to work. In my markup I have:
<asp:Menu SkinID="modulesMenu" DataSourceID="modulesSource" runat="server" ID="ModulesMenu"
OnMenuItemDataBound="ModulesMenu_MenuItemDataBound">
<StaticItemTemplate>
<div>
<asp:HyperLink ID="HyperLink2" CssClass="moduleName"
NavigateUrl='<%# ((MenuItem)Container.DataItem).NavigateUrl %>'
Text='<%# ((MenuItem)Container.DataItem).Text %>' runat="server" />
</div>
</StaticItemTemplate>
<DynamicSelectedStyle ForeColor="Red" Font-Bold="true" />
</asp:Menu>
This is the c# side
protected void ModulesMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
SiteMapNode siteMapNode = (SiteMapNode)e.Item.DataItem;
string pathStringFormat = IsNodeActive(siteMapNode) ? ConfigurationManager.AppSettings["ModuleImagePathFormatString_Active"] : ConfigurationManager.AppSettings["ModuleImagePathFormatString_Inactive"];
e.Item.ImageUrl = String.Format(pathStringFormat, siteMapNode["imageName"]);
if (IsNodeActive(siteMapNode))
{
e.Item.Selected = true;
}
}
private bool IsNodeActive(SiteMapNode siteMapNode)
{
if (SiteMap.CurrentNode != null)
{
return (SiteMap.CurrentNode.Equals(siteMapNode) || SiteMap.CurrentNode.IsDescendantOf(siteMapNode));
}
return false;
}