Click here to Skip to main content
Licence GPL3
First Posted 18 Sep 2007
Views 62,882
Downloads 1,107
Bookmarked 66 times

Create an ASP.NET Rounded Panel WebControl

By | 25 Sep 2007 | Article
A custom WebControl that emits CSS to create a stylable ASP.NET panel with rounded corners.
Screenshot - RoundedPanel.jpg

Introduction

There are perhaps hundreds of ideas on the internet regarding how to make those sleek rounded "Web 2.0" panels (as depicted above) that can be seen on millions of websites these days. Undoubtedly, there are many server controls that do the same thing. I've seen some good, and most bad. This was my attempt at creating a sleek solution to what certainly should be a simple problem.

Background

Because there is no one "right" answer to making rounded corners, I opted for what I deemed to be the simplest and highest performance solution. From a scalability standpoint, it didn't make sense to have to do it with images. While I'm a fan of JavaScript, I wanted to plan for the times when the client has JavaScript turned off. Sure, it's rare, but it happens. Finally, I wanted to encapsulate all of this functionality into a drag-n-drop server control that would require minimal configuration in Visual Studio and, of course, be cross-browser compliant.

Because I'm no CSS genius, I looked around on the web to find a few solutions that did what I wanted to do. I really just wanted pure CSS, and, as I said above, no images or JavaScript. I found exactly what I wanted on Stu Nicholls' website, CSSPlay. He had an elegant CSS solution that seemed like it would work well inside of a web control. And, since he didn't mind anyone using his code, I thought this would be the best approach. Thanks, Stu!

Inside the Code

The control itself contains only five properties and one overridden method. I allow the user to configure the Panel Border Color, Panel Text Color, Panel Inner Background Color, Panel Outer Background Color and the text inside of the panel. Sure, you can nest controls in the panel (it inherits from the ASP Panel control), but sometimes you don't want to have to configure a label control. Setting the text makes it simpler.

The method simply overrides the RenderContents method to output the CSS I needed nested in style tags, and the divs and nested tags that comprise the panel. I changed the CSS to reference elements by its unique Client ID, so it becomes feasible to have several panels on a page that have varying styles (as in the screenshot above). All the rest of the design functionality is inherited from the Panel control, including visibility.

On a final note, I made sure to render any child controls between paragraph tags. It makes the final panel rendering nice and neat. Yes, you can also use output.WriteBeginTag("p"); if you so choose. I like output.Write();

if (HasControls())
{
     output.Write("<p>");
     this.RenderChildren(output);
     output.Write("</p>");
}

Using the Code

I included the output DLL (Web2Controls.dll) in the downloadable source as well as the code file. You can simply add it to your toolbox, drag the control on your page, and start adding nested controls. All of the properties are located under the "Rounded Panel" category, and have defaults set. The panel also accounts for differing height and width units. You can use percentages or pixels with either. Because it inherits its designer attributes from the Panel control, you won't get a graphic representation of the panel (color and style wise) until you run it. It will simply display as any Panel would, and you can drag or drop controls on it.

I tested this in IE6, IE7 and Firefox, and didn't have any problems. I don't have Opera or Safari installed locally, so if any of you test with those platforms, I'd be interested to hear your results.

Happy UI'ing!

History

  • 9/25/07 - Removed a stray paragraph tag that was in for testing
  • 9/22/07 - Added closing div tag to output rendering

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

UsualDosage

Web Developer

United States United States

Member

I have been an ASP.NET/C# Programmer for about 7 years, specializing in business applications for financial institutions. I formerly wrote business applications for mortgage banking front-ends in C++ before switching to the .NET Framework, which I program in almost exclusively, now, except for my occasional contract dalliances in PHP and MySQL, which I really like. I especially enjoy graphic design, and web work.

In my spare time I run the local internet radio portal Jaxrockradio.com.

I have long moonlighted as an ANSI C programmer for several online MUDs (still a hobby of mine), and probably will continue to as long as they let me.

You can view my blog by visiting http://www.usualdosage.com.

My site design portfolio is located at http://design.usualdosage.com


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWhy the label cannot show inside the rounded panel PinmemberYee Cheing Seah18:12 24 Jun '09  
GeneralRe: Why the label cannot show inside the rounded panel PinmemberYee Cheing Seah18:15 25 Jun '09  
GeneralRe: Why the label cannot show inside the rounded panel Pinmemberwasanaeng8:15 4 Aug '09  
GeneralRe: Why the label cannot show inside the rounded panel PinmemberMember 41521840:18 29 Jun '11  
GeneralFix Rendering Child controls bug Pinmemberdreammaker_tr3:42 27 Nov '07  
GeneralNested Controls not working for me Pinmemberrlax312:04 15 Nov '07  
GeneralRe: Nested Controls not working for me PinmemberSteven Szelei20:07 11 May '10  
QuestionBorder Width PinmemberBen Millspaugh7:03 23 Oct '07  
AnswerRe: Border Width PinmemberUsualDosage8:08 23 Oct '07  
QuestionData Binding Problem PinmemberBen Millspaugh6:49 23 Oct '07  
GeneralRe: Data Binding Problem Pinmemberstixoffire2:04 14 Dec '07  
GeneralProblems with Updatepanels PinmemberWayne Gibson23:06 15 Oct '07  
GeneralRe: Problems with Updatepanels Pinmemberstixoffire2:00 14 Dec '07  
GeneralReally nice idea, but... Pinmemberintooitive3:02 25 Sep '07  
GeneralRe: Really nice idea, but... PinmemberUsualDosage4:58 25 Sep '07  
GeneralRe: Really nice idea, but... Pinmemberintooitive8:08 25 Sep '07  
GeneralRe: Really nice idea, but... PinmemberUsualDosage9:27 25 Sep '07  
GeneralQuestion on Foregroundcolor PinmemberDaProgramma2:29 25 Sep '07  
GeneralRe: Question on Foregroundcolor PinmemberUsualDosage5:00 25 Sep '07  
GeneralDoes not render properly PinmemberDaProgramma10:40 24 Sep '07  
GeneralRe: Does not render properly PinmemberUsualDosage11:09 24 Sep '07  
GeneralRe: Does not render properly PinmemberDaProgramma19:44 24 Sep '07  
GeneralRe: Does not render properly PinmemberUsualDosage2:14 25 Sep '07  
GeneralRe: Does not render properly PinmemberDaProgramma2:26 25 Sep '07  
GeneralPanel clash with DropDownList PinmemberMohammadHafizullah15:21 23 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 25 Sep 2007
Article Copyright 2007 by UsualDosage
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid