1

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#code used to identify the selected item

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;
}
ksg
  • 3,927
  • 7
  • 51
  • 97

2 Answers2

0

I can give you an idea. In ModulesMenu_MenuItemDataBound compare the item url and physical file of the request by the following code

  Path.GetFileName(Request.PhysicalPath).ToUpper()

You can use can compare both and set selected css-class using compare as follows

lstrMenuUrl.ToUpper().Replace(" ", "").Contains(filename.ToUpper())

where lstrMenuUrl is the current menu which is clicked.

Here is a link which may help you

Highlight Selected Tab in ASP.Net Menu

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

I wrongly set the attribute .The attribute to set was

 <Staticselectedstyle ForeColor="Red" Font-Bold="true"    />

instead of

<DynamicSelectedStyle ForeColor="Red" Font-Bold="true" />
ksg
  • 3,927
  • 7
  • 51
  • 97