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,
DovbermanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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

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
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
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.