?
Solved

ASP.NET MenuItem CSS Class

Posted on 2008-11-14
7
Medium Priority
?
3,468 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:kimmiko
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:moseack
ID: 22970107
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
 
LVL 1

Author Comment

by:kimmiko
ID: 22972163
menu.Controls[i].CssClass  is invalid because the MenuItem does not have a CssClass property.  
0
 
LVL 7

Expert Comment

by:moseack
ID: 22973962
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:kimmiko
ID: 22975688
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
 
LVL 7

Expert Comment

by:moseack
ID: 22976440
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
 
LVL 1

Author Comment

by:kimmiko
ID: 22976641
menu.Controls.Count is still 0 on PreRender
0
 
LVL 1

Accepted Solution

by:
kimmiko earned 0 total points
ID: 23131787
The only way I found to make this possible is to use the CSS Friendly control adapters:

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question