I've been trying to find a way to style the asp.menu control for a while. Many of the examples online were not helpful as the attributes for setting styles does not work (ie StaticMenuItemStyle-CssClass="SOMECLASS"). So I was hoping there was a way to do it programmatically? Please help.
protected void Menu_MenuItemDataBound(object sender, MenuEventArgs e)
{
if (e.Item.NavigateUrl.Trim() == _currentUrl.Trim())
{
// Something like this
e.Item.CssClass = "SOMECLASS";
}
}
EDIT I've done a work around for my Site Navigation. If there is a way to use ASP.NET Menu Style Attributes let me know!
protected void Menu_MenuItemDataBound(object sender, MenuEventArgs e)
{
MenuItem menuitem = (MenuItem)e.Item;
if (menuitem.NavigateUrl.Trim() == _currentUrl.Trim())
{
if (menuitem.Depth == 1)
{
menuitem.Text = "<span class=\"active" + menuitem.Depth + " selectedlevel1\">" + menuitem.Text + "</span>";
}
else
{
menuitem.Text = "<span class=\"active" + menuitem.Depth + " selectedlevel2\">" + menuitem.Text + "</span>";
}
while (menuitem.Parent != null)
{
menuitem = menuitem.Parent;
String title = menuitem.Text;
title = title.Replace("<span>", "");
title = title.Replace("</span>", "");
menuitem.Text = "<span class=\"active" + menuitem.Depth + "\">" + title + "</span>";
}
}
else
{
menuitem.Text = "<span>" + menuitem.Text + "</span>";
}
}