SIGN UP MEMBER LOGIN:    
ARTICLE

CSS Menu in a .Net Master Page

Posted by Andrew Fenster Articles | Current Affairs October 06, 2010
In this article you will learn how to use a CSS menu in a .Net Master Page.
Reader Level:
Download Files:
 

A quick search on the Internet for "CSS menus" reveals hundreds of websites providing free CSS based menus: horizontal menus, vertical menus, menus with tabs, menus with dropdowns, etc. Almost all of these menus operate on the same basic principle: they take an HTML unordered list and they apply CSS styles. That is, the HTML looks something like this:

Image.gif

The CSS makes this look like a menu or set of tabs:

Image2.gif

The CSS has one style for the active tab (Link 3) and another style for all the other tabs. The HTML indicates which tab is active by assigning it a special CSS class:

Image3.gif

This is where the difficulty arises when you're using a Master Page. How do you set the class for the active tab?

You can put the basic HTML list in the Master Page, but you will have to write code to set the class for the active tab. There are many ways to do this. I do it with JavaScript.

The first step is to give each list item an ID.

Image4.gif

Add the following to the master page:

Image5.gif

Each of the content pages (page1.aspx, page2.aspx, etc.) should implement the Master Page and include the following. On each page, this hidden span indicates which menu tab should be active.

Image6.gif
Now we'll add some Javascript to the Master Page. Each time the Master Page loads, the Javascript code will find the span called tabid. The text of this span provides the ID of the menu tab which should be active. The Javascript code will find the menu tab and give it the "active" class.

Image7.gif

In jQuery, the Javascript looks like this:

Image8.gif

The downloadable code sample uses a CSS menu from Dynamic Drive CSS Library (http://www.dynamicdrive.com/style/).  Dynamic Drive CSS Library has many different CSS menus which you can download and use. In my example, I used the DD Color Tabs CSS(http://www.dynamicdrive.com/style/csslibrary/item/dd-color-tabs/P10/).
 

Login to add your contents and source code to this article
share this article :
post comment
 

the above code is not working in fire fox

Posted by sathesh R May 30, 2011

when I click on CssMenu.zip it gives following error XML Parsing Error: no element found Location: http://www.c-sharpcorner.com/uploadfile/afenster/3737/Default.aspx Line Number 1, Column 1: Please help me....

Posted by vaibhav fegade Mar 23, 2011

I just downloaded the sample project and ran it again. I ran it using the jQuery code. I ran it using the regular JavaScript code. The code looks fine. It worked perfectly. All I can suggest is that you try again. Download the project and run it without modifying anything. I'm sorry you're having problems.

Posted by Andrew Fenster Feb 21, 2011

I am also not getting this to work. I ended up downloading the sample file and commented out the j-query so I could see how the javascript was working, only it didn't work at all with the sample file. The tabs didn't show a current state. I think there must be a discrepancy somewhere...

Posted by Allison Szasz Feb 18, 2011

Yes I noticed these things, I think I may just need to make a few more ammendments. I keep you updated, thank you

Posted by Ben Johnson Jan 05, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor