Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
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.

Page Views : 190767
Downloads : 7875
Rating :
 Rate it
Level : Advanced
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CurvedMenuProjects.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Rao Chowdary on Dec 10, 2010
Hi Mike,

This menu is pretty good.I  play around it but when integrated with master pages  it giving the following errors message.


CS0426: The type name 'Site1' does not exist in the type 'System.Web.UI.WebControls.Menu'

 Line 145: [TemplateContainer(typeof(Menu.Site1))]
 
Line 146: [TemplateInstanceAttribute(System.Web.UI.TemplateInstance.Single)]



Site1 is master page name you can please help me how to resolve this issue.


Thanks
Rao
 [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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
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 | 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 | 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 | 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 | Modify 
it is not working in 2003 by srinivasan On November 3, 2007
Hello friends i want code for 2003
Reply | Email | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | Modify 
Re: How to use this? by SenthilKumar On December 2, 2009
Hey..I am not sure whether you got answer for this. Anyway here you go. Sitemap file contains the menu and related urls. You can change the URL with the specific page.

Thanks,
Senthil Kumar
Reply | Email | Modify 
Re: Re: How to use this? by Pham On August 20, 2010
any one help me, when I run CurvedMenuProjects/MenuRoundedTabsWebSite project, it notify: Error    1    It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.    D:\CurvedMenuProjects\MenuRoundedTabsWebSite\Web.Config    18   
pls help me resolve this error, thank a lot
Reply | Email | Modify 
Where are my submenu items? by Matt On February 26, 2010
I was able to adjust the appearance just fine be making the changes and recompiling the dll as needed, along with making changes to the css file... however, my submenu items will not show.
I am dynamically building my menu at runtime, based on the user's security access as stored in my db... and it all worked fine when I was using a asp menu control.
All I did was change backgroung/text colors in your code... any ideas?
MC
Reply | Email | Modify 
GOOD by Radio On November 7, 2010
Nice work!!!
Reply | Email | Modify 
menu Controls by Rao On December 10, 2010
Hi Mike, This menu is pretty good.I play around it but when integrated with master pages it giving the following errors message. CS0426: The type name 'Site1' does not exist in the type 'System.Web.UI.WebControls.Menu' Line 145: [TemplateContainer(typeof(Menu.Site1))] Line 146: [TemplateInstanceAttribute(System.Web.UI.TemplateInstance.Single)] Site1 is master page name you can please help me how to resolve this issue. Thanks Rao
Reply | Email | Modify 
layout.js by Henry On February 16, 2011
Can someone give me the link to source code for layout.js. Looks like the zip file does not have source for the custom control.
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.