to StackOverflow
to StackOverflow

Reputation: 124696

How to control CSS class applied to ASP.NET 4 Menu with RenderingMode=List

I am using an ASP.NET 4.0 Menu control with RenderingMode=List and am struggling with creating the appropriate CSS. Each menu item is represented by an <li> tag that contains a nested <a> tag with what appear to be fixed class names:

... etc...

What I want to do is to is to prevent the currently selected menu item from being "clickable". To do so I tried using:

if (menuItem.Selected) menuItem.Selectable = false;

This has the desired effect of removing the href attribute from the <a> tag but also removes the class attribute - and as a result my CSS can not identify what level the menu item belongs to!

Looks to me like a possible bug, but in any case I can't find any documentation describing what CSS class names are used, nor whether there is any way to control this (the old Style properties don't appear to have any effect). Ideally I would like to have "level" class attributes on the <li> tags, not just the nested <a> tags.

UPDATE

I've taken a look at the System.Web source with Reflector, and it appears that it does explicitly skip outputting CSS attributes if Selectable=false. The following is extracted from MenuRendererStandards.RenderItemLinkAttributes:

...
if (!item.Selectable)
{
    return needsAccessKey; // !! exits without setting class attribute
}
if (item.Selected)
{
    cssClass = cssClass + " selected";
}
writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
...

Upvotes: 1

Views: 2139

Answers (2)

Butti
Butti

Reputation: 377

After I found this out on my own, I got the explanation from the UPDATE of the original post. Thank you for this! I check for the current .net implementation and have good news on this :)

   ...
   if (!item.Selectable){
      writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
      return needsAccessKey;
   }

  // Selected
  if (item.Selected) {
      cssClass += " selected";
  }
  writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);

I hope this is helpful for somebody.

Upvotes: 0

Homa
Homa

Reputation: 132

Check out HtmlTextWriterAttribute class in System.Web.UI namespace, so that you write the attributes that you want.

I hope it help you.

Upvotes: 1

Related Questions