Handling Images in Themes

An entry about asp.net Publication date 11. March 2007 23:41

The standard way of working with image resources in themes, is to define the ImageUrl property in a skin file within the theme. Personally I've never really taken to the whole skin system, and seldom use themes for more than loading a different set of stylesheets. However, one thing that the skin file can do that a stylesheet can't, is specify the ImageUrl property of an Image control (or the src of an <img>, if you will).

To work around this issue without having to resort to skinning all my Image controls, I created a custom control that I've called ThemeImage. Basically, it works just like the Image control, only it lets me specify an ImageUrl path that is relative to the current theme. So for instance, if I have:

<fk:ThemeImage
    runat="server"
    ImageUrl="Images/Header.png" />

When being rendered, the ImageUrl is translated into ~/App_Themes/{CurrentTheme}/Images/Header.png, and the header image for the current theme is displayed to the user. Personally, I find this approach much cleaner and easier to work with than having to maintain markup across two different files just to get my images to display.

Implementing the ThemeImage control is almost embarrassingly simple (a telltale sign of a well designed API) – all we have to do is create a new class, inherit it from System.Web.UI.WebControls.Image and override the Render method:

protected override void Render(HtmlTextWriter writer)
{
string oldUrl = this.ImageUrl;
this.ImageUrl = String.Format("~/App_Themes/{0}/{1}", this.Page.Theme, this.ImageUrl);
base.Render(writer);
this.ImageUrl = oldUrl;
}

And that's it!

Look out for more posts on how to improve Themes in ASP.NET 2.0 soon – one particularly interesting in the pipeline deals with how to let include different MasterPages in different Themes.

Be the first to rate this post

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.

NDC 2010

The conference to attend this summer happens June 16th-18th in Oslo, Norway. Are you going? Be sure to catch my talk on AOP while you're there!

 

Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS