• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1677
  • Last Modified:

How to apply a css stylesheet to an asp menu

I used the following example in an attempt to apply a css style sheet to a menu.
http://jobinjohn.blogspot.com/2011/06/styling-aspnet-menu-control-with-css.html

This is my aspx code snippet:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 <asp:Menu ID="Menu1" CssClass="MYMenuClass"
        Orientation="Horizontal"
        MaximumDynamicDisplayLevels="3"
        runat="server">


        <Items>
            <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
                <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
                <asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item"></asp:MenuItem>
                <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
            </asp:MenuItem>

This is my css code snippet:

.MYMenuClass {
    color: Green;
    background-color: #00FFFF;
}

.MYMenuClass ul {
    background: #00FFFF;
}

.MYMenuClass ul li {
    background: #00FFFF none no-repeat;
    text-align: center; /* set width if needed.*/
    width: 200px;
}

.MYMenuClass ul li a
{
    color: black;
    padding: 4px 2px 4px;
    padding-left:8px !important;
    border:1px solid #648ABD;
    border-bottom: 0
   
}

.MYMenuClass ul li a:hover
{
    background-image: none;
}

.MYMenuClass ul li a:hover
{
    color: White;
}

Nothing in the css file is applied to the menu.

What am I missing?

Thanks,
0
Dovberman
Asked:
Dovberman
  • 8
  • 5
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
Look at the source in the browser and see if your classes are listed in the menu items.  Your menu is generated at the server but CSS is applied in the browser.  If the classes aren't listed in the menu items in the browser, they won't be applied.
0
 
Lokesh B RDeveloperCommented:
Hi,

Just Run the Page and you can see the CSS applied to your Menu items in browser.
0
 
DovbermanAuthor Commented:
This is my source:

<asp:Menu ID="Menu1" CssClass="MYMenuClass"
         Orientation="Horizontal"
         MaximumDynamicDisplayLevels="3"
         runat="server">

?? How do I list the class here?
         <Items>
             <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item">
                 <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
                 <asp:MenuItem NavigateUrl="#" Text="New Item New Item" Value="New Item">

</asp:MenuItem>
                 <asp:MenuItem NavigateUrl="#" Text="New Item" Value="New Item"></asp:MenuItem>
             </asp:MenuItem>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DovbermanAuthor Commented:
The stylesheet  Menu1.css is in the list.
0
 
Lokesh B RDeveloperCommented:
Hi,

Do you want to generate the menu dynamically?
0
 
DovbermanAuthor Commented:
I do not care.  I just want the style sheet properties to be applied.
0
 
Lokesh B RDeveloperCommented:
Hi,

Check the following code.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .MYMenuClass {
            color: Green;
            background-color: #00FFFF;
        }

            .MYMenuClass ul {
                background: #00FFFF;
            }

                .MYMenuClass ul li {
                    background: #00FFFF none no-repeat;
                    text-align: center; /* set width if needed.*/
                    width: 200px;
                }

                    .MYMenuClass ul li a {
                        color: black;
                        padding: 4px 2px 4px;
                        padding-left: 8px !important;
                        border: 1px solid #648ABD;
                        border-bottom: 0;
                    }

                        .MYMenuClass ul li a:hover {
                            background-image: none;
                        }

                        .MYMenuClass ul li a:hover {
                            color: White;
                        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Menu ID="navMenu" runat="server" Orientation="Horizontal" CssClass="MYMenuClass">
            <Items>
                <asp:MenuItem Text="Home" NavigateUrl="home.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Categories"></asp:MenuItem>
                <asp:MenuItem Text="Products"></asp:MenuItem>
                <asp:MenuItem Text="About Us" NavigateUrl="AboutUs.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Contact Us"></asp:MenuItem>
            </Items>
        </asp:Menu>
    </div>
    </form>
</body>
</html>

output.PNG
0
 
DovbermanAuthor Commented:
I understand what you did.

The menu control is not used.  The css code is in the html file (myfile.aspx).

What I do  not understand is why the examples using a style sheet (css file) did not work for me.
0
 
Lokesh B RDeveloperCommented:
Hi,

Check whether your css file is referenced properly.

It will work with the css file as well. See the below sample code of ASPX.

CSS file is in my Content Folder.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.file.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <link href="../Content/MyMenuCss.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Menu ID="navMenu" runat="server" Orientation="Horizontal" CssClass="MYMenuClass">
            <Items>
                <asp:MenuItem Text="Home" NavigateUrl="home.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Categories"></asp:MenuItem>
                <asp:MenuItem Text="Products"></asp:MenuItem>
                <asp:MenuItem Text="About Us" NavigateUrl="AboutUs.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Contact Us"></asp:MenuItem>
            </Items>
        </asp:Menu>
    </div>
    </form>
</body>
</html>
0
 
DovbermanAuthor Commented:
That makes sense.  I was wondering how to reference the css style sheet file.
0
 
DovbermanAuthor Commented:
The dynamic form works.

The css style sheet reference does not work for  either of the references shown below.

<link href="C:\Users\David\Documents\Visual Studio 2012\Projects\StockPickerMax\StockPickerMax\Menu1.css" rel="stylesheet" />

<link href="~/Menu1.css" rel="stylesheet" />

I will use the dynamic method but prefer the style sheet method.

Is my css file correct ?


   .MYMenuClass {
    color:#008000;
    background-color: #00FF00;
}

             .MYMenuClass ul {
                 background: #00FFFF;
             }

                 .MYMenuClass ul li {
                     background: #00FFFF none no-repeat;
                     text-align: center; /* set width if needed.*/
                     width: 200px;
                 }

                     .MYMenuClass ul li a {
                         color: black;
                         padding: 4px 2px 4px;
                         padding-left: 8px !important;
                         border: 1px solid #648ABD;
                         border-bottom: 0;
                     }

                         .MYMenuClass ul li a:hover {
                             background-image: none;
                         }

                         .MYMenuClass ul li a:hover {
                             color: White;
                         }

Thanks for all your help.
0
 
Lokesh B RDeveloperCommented:
Capture.PNGHi,

Just Drag & Drop the CSS file from the Folder to ASPX page in Visual Studio.

As shown in the below image.
0
 
DovbermanAuthor Commented:
That worked:

<head id="Head1" runat="server">
     <title></title>
     <link href="Menu1.css" rel="stylesheet" />

 </head>

Thank you.
0
 
DovbermanAuthor Commented:
Excellent. Provided just what I needed in a timely fashion.
0
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now