ASP.NET Cafe
Tips and Tricks

asp.net themes and custom controls

Sunday, 13 January 2008 14:32 by Dmitriy

There are very nice thing in asp.net called themes. This is a folder, located under App_Themes, inside this folder you can put images, css files and .skin files. The skin file allows you to set some asp.net controls properties theme-wide. It looks like
<asp:Button runat="server" BackColor="#ff46a1" BorderWidth="1px" BorderColor="#666666" ForeColor="#666666" BorderStyle="Solid" />
But what to do with custom controls? When you are writing custom control you need to do several things:

First thing you need to do - add line to assemblyinfo.cs,
[assembly: TagPrefix("SampleNamespace", "sample")]
to define tag prefix for your control.

Now you can use <#@ Register... inside your skin file, and write in skin file something like this:
<sample:SampleControl runat="server" BackColor="#ff46a1"
BorderWidth="1px" BorderColor="#666666" ForeColor="#666666" CustomImageProperty="images/bg.gif" BorderStyle="Solid" />

This is good, but one problem exist. Skin file allows you to setup images, and these images are relative to theme directory. But CustomImageProperty by default remains relative to page, and this is a problem. How to solve it? We need to use [UrlProperty()] attribute in our custom control. UrlPropertyAttribute shows asp.net that this property is a path, and in case of skins should be substituted. This way path will be relative to theme's folder, and we are happy.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   ASP.NET
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (2) | Comment RSSRSS comment feed

Comments

February 28. 2008 12:18

Rob

This is pretty easy to show the image editor, is thier an editor for FolderBrowserDialog to choose a path location instead of a specific file? I have embedded an image in a custom control and would like the end user to choose where to save the image by choosing a path.

Rob

March 3. 2008 08:50

Rob

I am still reading the MSDN documentation about UITypeEditor but It looks like there is something different about them in 2008 versions, the same projects from codeproject that work to create an ellipsis (modal dialog) when the UITypeEditor class is overridden in 2005 do not wok in the 2008 versions.

Creating a modal form will result in a string/text property ... it's almost like the overrides don't work. I tried a test overriding a Color property to turn it into a dropdownlist instead of modal, guess what...didn't work in 2008...did work in 2005.

When I find the difference I'll post an article about it.

Rob

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Preview

December 4. 2008 12:32