ASP.NET MenuItem CSS Class

I am using System.Web.UI.WebControls.Menu bound to an XMLDataSource for my menus.  I have a menu structure like this:

Category 1
..SubCategory1
..SubCategory2
Category2
..SubCategory1
Category3

I currently have a CSS Class on all categories (including SubCategories) called "categoryItem", plus one on the main level called "categoryLevel1" and one on the subcategories called "categoryLevel2".  This is all accomplished using LevelMenuItemStyles.

In addition, I need to give each main level category a unique CSS class like "categoryNumber1", "categoryNumber2", "categoryNumber3".  How can I accomplish this?
LVL 1
kimmikoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:
The only way I see is to manipulate the control tree after data-binding (with the DataBound event)


<script runat="server">
 
  void NavigationMenu_DataBound(Object sender, EventArgs e)
  {
     Menu menu = (Menu)sender;
     for (int i = 0; i < menu.Controls.Length; ++i)
     {
       menu.Controls[i].CssClass += " xxx" + i.
     }
  }
 
</script>
 
<html  >
  <head runat="server">
    <title>Menu MenuItemDataBound Example</title>
</head>
<body>
    <form id="form1" runat="server">
 
      <h3>Menu MenuItemDataBound Example</h3>
 
      <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10" 
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        ondatabound="NavigationMenu_DataBound"    
        runat="server">
...

Open in new window

0
kimmikoAuthor Commented:
menu.Controls[i].CssClass  is invalid because the MenuItem does not have a CssClass property.  
0
Refael AckermannCommented:
Let me amend that

<script runat="server">
 
  void NavigationMenu_DataBound(Object sender, EventArgs e)
  {
     Menu menu = (Menu)sender;
     for (int i = 0; i < menu.Controls.Length; ++i)
     {
       WebControl wc = menu.Controls[i] as WebControl;
       if (wc == null) continue;
       wc.CssClass += " xxx" + i;
     }
  }
 
</script>

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

kimmikoAuthor Commented:
menu.Controls.Length doesn't work, but I tried .Count instead and the count is 0 at the time the DataBound event fires so the CssClass is not set.
0
Refael AckermannCommented:
You could try the PreRender event:

  void NavigationMenu_PreRender(Object sender, EventArgs e)
  {
...


     <asp:menu id="NavigationMenu"
        staticdisplaylevels="2"
        staticsubmenuindent="10"
        orientation="Vertical"
        target="_blank"
        datasourceid="MenuSource"
        onprerender="NavigationMenu_PreRender"    
        runat="server">
0
kimmikoAuthor Commented:
menu.Controls.Count is still 0 on PreRender
0
kimmikoAuthor Commented:
The only way I found to make this possible is to use the CSS Friendly control adapters:

http://www.codeplex.com/cssfriendly
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.