Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Visual Studio .NET » Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners

Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners

The ASP.NET Menu Control normally produces tabs which have rectangular edges. The techniques presented extend the control to create tabs with rounded corners.

Technologies: .NET 1.0/1.1, ASP.NET 1.0, Common Controls, Controls,Visual C# .NET
Total downloads : 5407
Total page views :  129250
Rating :
 4.75/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CurvedMenuProjects.zip
 
Become a Sponsor




Introduction

Image1.gif

Ever get tired of the square tabs on the .Net Menu Control? I did. This article combines several cool works done by others on the web to extend the .Net 2.0 Menu control to produce rounded tabs using no images for the corners.

Technologies explored in this article include:

  1. Using CSS styles to produce rounded corners without the need for corner images.  See More Nifty Corners  for the technique used.
  2. Inheriting the Menu control and overloading it's Render method to produce CSS friendly html elements in which to apply the rounded corner technique above. I borrowed a lot of the code from ASP.NET 2.0 CSS Friendly Control Adapters 1.0 but instead of an Adapter I transformed it into a custom web control.
  3. Packaging this customized Menu control, along with it's CSS styles and javascripts into a separate dll that can be shared between projects.  These CSS styles and javascripts are included into the dll as Web Resources.


Approach Used To Create The Rounded Corners on the Menu's Tabs

Nothing makes a web site look polished like rounded corners. This includes the menu tabs. I've seen many techniques for creating rounded corners but none as elegant as More Nifty Corners . This article shows you how to produce rounded edges w/o the use of corner images. 

After reading this article, I was fired up to apply the techniques to menu tabs, however the aspx menu control uses tables which are not compatible with this technique. So I needed a method of changing these table tags to something that could be styled more easily. After, changing the tags, there was no need to change the main javascript file or the CSS file the technique uses. However, I did want to package these scripts into the control's dll to make it easier to use.

Creating a CSS friendly Menu Control is a matter of inheriting the ASP Menu control and overloading it's "Render" method to generate CSS friendly code. Fortunately, I came across: ASP.NET 2.0 CSS Friendly Control Adapters 1.0  that saved me from  creating the code from scratch. This provided code for creating an Adapter for the Menu control that generated CSS Friendly html output. Adapters are cool but there seemed to be more configuration required than if you just had a custom control that did the same thing.  Fortunately the way an adapter works is pretty similar to how a custom control works; so I was able to copy the code into an overloaded Render method of my Custom Control that was derived from the Menu Control.  Only a few minor tweaks were required to complete the transformation from adapter to custom web control. I was also able to use all of the client side javascript w/o any changes or any need for additional client side code. 

I made a several changes to the CSS style sheet. Mostly deleting the tags, which weren't being used to make it a little easier to follow. 

My goal was to make this rounded tab menu easy to use, without a lot of configuration. It seemed the way to do this was to put as many of the scripts the control uses within the control itself. I elected to put the server side code, the CSS and javascript fils to create the rounded corners and the javascript file for the menu into the control's dll. This dll can be easily referenced from a web project. When the control is used these scripts are automatically included on the page. In addition, the web project will need to include one CSS file to format the menu (colors of the tabs ect) as well as two small images that are used to indicate which menu item's have child menu items.

Download Files

The download includes two projects. One sample web project "MenuRoundedTabsWebSite" which uses the custom control and one project containing the custom menu control.  The web project was created using Visual Web Developer 2005 and the custom menu control was created using Visual C# 2005 Express Edition.  To open up the web site in Visual Web Developer go to the file menu , Open Web Site then navigate to the "MenuRoundedTabsWebSite" folder.

The Code

I'll go over the main features of the code, download the code if you want to see all of the code:

Including script files as Web Resources

The script files, which are included as web resources in the control have their build action set to "embedded resource" (see figure 1).

Image2.gif

Figure 1- Files included as web resources have their build action set to "Embedded Resource"

Also for these web resources to be available the following lines of code needs to be added to AssemblyInfo.cs

Next "Assembly Info.cs" file needs to have the following added to it's end(Figure 2) :

[assembly: System.Web.UI.WebResource("CustomControls.Resources.Menu.css", "text/css")]

[assembly: System.Web.UI.WebResource("CustomControls.Resources.layout.css", "text/css")]

[assembly: System.Web.UI.WebResource("CustomControls.Resources.niftyCorners.css", "text/css")] 

[assembly: System.Web.UI.WebResource("CustomControls.Resources.layout.js", "text/javascript", PerformSubstitution = true)]

[assembly: System.Web.UI.WebResource("CustomControls.Resources.MenuAdapter.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("CustomControls.Resources.nifty.js", "text/javascript", PerformSubstitution = true)]

Figure 2 - Coded to be added to Assembly Info.cs

These web resources are used in the control's overload "Init()" method (See figure 3)

 

Figure 3 shows the overloaded Init() method and the private RegisterScripts() method it calls.

/// <summary>
/// Call when the control is first created
/// </summary>
/// <param name="e"></param>

protected override void OnInit(EventArgs e)

{

    base.OnInit(e);

    //check to make sure we don't add the scripts twice; if more than one control on the page

    if (ViewState["MenuRegisterScripts"] == null)

    {

        RegisterScripts();

    }

    ViewState["MenuRegisterScripts"] = true; 

}

/// <summary>

/// Load the scripts from the WebResources

/// </summary>

private void RegisterScripts()

{

    string csslink=null;

    LiteralControl include = null;

 

    // add main javascript file required to round the corners to the header

 

    HtmlGenericControl script = new HtmlGenericControl("script");

    script.Attributes["type"] = "text/javascript";

    script.Attributes["src"] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomControls.Resources.nifty.js");

    this.Page.Header.Controls.Add(script);         

           

    // add the css files to create the rounded corners

    csslink = "<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "CustomControls.Resources.niftyCorners.css") + "' />";

    include = new LiteralControl(csslink);

    this.Page.Header.Controls.Add(include);           

      

    csslink = "<link rel='stylesheet' type='text/css' href='" + Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "CustomControls.Resources.niftyPrint.css") + "' />";

    include = new LiteralControl(csslink);

    this.Page.Header.Controls.Add(include);          

 

     // register the javascript code that specifies the format for these rounded corners

    // this script also specifies the colors for the corners

    if(!Page.ClientScript.IsClientScriptIncludeRegistered(Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "CustomControls.Resources.layout.js")))

    {

        this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Layout", Page.ClientScript.GetWebResourceUrl

        (this.GetType(), "CustomControls.Resources.layout.js"));

    }

 

    // register the javascript code that specifies the format for these rounded corners

    // this script also specifies the colors for the corners

    if (!Page.ClientScript.IsClientScriptIncludeRegistered(Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "CustomControls.Resources.MenuAdapter.js")))

    {

        this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "MenuAdapter", Page.ClientScript.GetWebResourceUrl

        (this.GetType(), "CustomControls.Resources.MenuAdapter.js"));

    }

 

    //add the css script used by the Menu to the header

    csslink = "<link rel='stylesheet' type='text/css' href='" +

    Page.ClientScript.GetWebResourceUrl(this.GetType(),

    "CustomControls.Resources.Menu.css") + "' />";

    include = new LiteralControl(csslink);

    this.Page.Header.Controls.Add(include);
}

Figure 3-Method's used to add the required scripts to the page.
 
Overloading the Render Method To Generate CSS Friendly Code

Figure 4 shows the over loaded "Render" method. The "RenderContents" method (not shown) loops through each of the menu items and creates html ul and li tags to separate them.  You can download the code to see the implementation of the "RenderContents" method.

protected override void Render(HtmlTextWriter writer)
{
    writer.WriteBeginTag("div");
    writer.WriteAttribute("class", "PrettyMenu");
    writer.WriteAttribute("id", this.ID);
    writer.Write(HtmlTextWriter.TagRightChar); 

    writer.WriteBeginTag("div");

    writer.WriteAttribute("class", "AspNet-Menu-Horizontal");

    writer.Write(HtmlTextWriter.TagRightChar);

           

    RenderContents(writer);

 

    writer.WriteEndTag("div");

    writer.WriteEndTag("div");
}


Figure 4 - Custom's Control Overloaded Render method

Changing the Colors for the Menu's Tab

By altering the layout.js file in the dll's Resources folder, you can change the menu's tab colors. 

Rounded("a.AspNet-Menu-Link-Parent","top","#E8BD8A","#7795BD","border #4B463B")

The third parameter should be the background color of what ever is containing the menu.  The forth parameter should be set to the color of the tabs and the final parameter should be the border color of tabs.

See More Nifty Corners  for additional instructions.

Configuring Your Site To Use Curved Tab Menu Control

When using this menu control for a web project, there is very little configuration required. First you set a reference to the dll containing the menu control. You register this custom control on the web page:

<%@ Register TagPrefix="Custom" Namespace="CustomConrols" Assembly="CustomControls" %>

You then use the control by adding the following:

<Custom:RoundedMenu ID="RoundedMenu1" runat="server" datasourceid="ExampleSiteMapDS" orientation="Horizontal" ></Custom:RoundedMenu>

Then you need to add the CSS file which formats the menu and a few small images which are used in this CSS file into your Theme folder:

Image3.gif 

Finally you need to tell your page where to find the above files:

<%
@ Page Language="C#"  Theme="basic" %>

Note: "Theme" is set to "basic" which is the sub folder containing these files.

That's about all that's required except for changing the MenuCurvedTab.css file to format the tabs to use your site's color schemes.

Conclusion

The code presented illustrates how a control's render method can be manipulated to alter the HTML output.  Also, it shows how cool scripts such as rounding the corners can be encapsulated into the custom control to make them more usable.

The customized menu control presented has a few limitations, i.e. if two menu's are added to the same page the format of both will be the same since the same CSS style sheet will be used for both.  Also, the menu is currently three layers deep.  If more layers are added then the CSS style sheet will need to be extended.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mike Clark
Mike is a software consultant with 10+ development experience who works on IT projects mainly for Microsoft and Boeing. Most of these projects have been intranet based web sites connected to a SQL database. In the last 6 years, Mike has focused on C# and SQL Server development.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CurvedMenuProjects.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Not all formatting in CSS by David On March 24, 2007
I was playing with this and found that some colors were hard-coded in layout.js (compiled in the DLL). Is there an easy way to break these out to non-compiled CSS so the DLL is completely reusable?
Reply | Email | Delete | Modify | 
Not work with IE version less than 7.0 by song On May 14, 2007
Thanks for your work. I used your dll in my proect, it works on IE7.0 and Firefox, but not work on IE version less than 7.0. Is it possible to fix this? Thanks Song
Reply | Email | Delete | Modify | 
strange by roog On June 16, 2007
the zip files contain 3 copies of every file... then when running the project I get the error that it can not find MenuRoundedTabsWebSite\WebResource.axd?d=OzTpxqoea5ICq-XQP8jFeSU8OaStwXnlRoP7gkhJRmbxXLVVPybuJWXEDUxOICgnZhLgZULw68Hs-X67RPpRNg2&t=633176300476718750, don't want to get into debugging and stuff, just wanted to run it and then see how it's done. Does anyone know what this error means?
Reply | Email | Delete | Modify | 
Re: strange by Paul On October 30, 2007
I replaced the repeated files when I extracted the archive, worked ok then. This would be useful to me if you could change the tab colour when it is selected, is this possible?
Reply | Email | Delete | Modify | 
it is not working in 2003 by srinivasan On November 3, 2007
Hello friends i want code for 2003
Reply | Email | Delete | Modify | 
I want code for .net 1.0 by srinivasan On November 3, 2007
I am worked now in asp.net 1.0 , i want code for 1.0, This code not working in 1.0,
Reply | Email | Delete | Modify | 
Very good by Srinivasa Rao N On December 30, 2007
Hi Mike i am glad to say this article is very good in future i may touch with ur skill sets. Regards, Srinivasa
Reply | Email | Delete | Modify | 
Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Mahesh On August 26, 2008
Hi, It doesnt work in IE 6.0. Can u pls suggest on this.. Rgds, Mahesh
Reply | Email | Delete | Modify | 
Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Mahesh On August 26, 2008
Hi, It doesnt work in IE 6.0. Can u pls suggest on this.. Rgds, Mahesh
Reply | Email | Delete | Modify | 
Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Mahesh On August 26, 2008
Hi, It doesnt work in IE 6.0. Can u pls suggest on this.. Rgds, Mahesh
Reply | Email | Delete | Modify | 
Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Mahesh On August 26, 2008
Hi, It doesnt work in IE 6.0. Can u pls suggest on this.. Rgds, Mahesh
Reply | Email | Delete | Modify | 
Re: Extending ASP.NET 2.0 Menu Control To Have Tabs With Rounded Corners by Phil On March 26, 2009
Was this ever fixed in IE 6?

Reply | Email | Delete | Modify | 
Master Page by Pradeep On March 4, 2009
I am not able to resize or to fix the contentplaceholder in the masterpages in asp.net pls give me solutions for this regards, Pradeep
Reply | Email | Delete | Modify | 
Great Work buddy by hitesh On April 1, 2009
Really a good work. i m student this helped me in making my final year project
Reply | Email | Delete | Modify | 
not working by sasas On April 8, 2009
i downloaded the zip file unzipped it and tried to run but it says invalid stylesheettheme value.Any help greatly welcomed
Reply | Email | Delete | Modify | 
Width of top level menu tab by sachin On June 3, 2009

I can not able to change the width of top level menu tab.please give me solution for the same.

Reply | Email | Delete | Modify | 
How to use this? by Srikanta On August 31, 2009
I got the rounded menu, but not sure how to use this? I mean suppose I click "JustForYou > Journalist" from menu, and I want to go to a specific page. How can I do this?

mail to : srikantak@yahoo.com

Thanks in advance.
-S
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved