Theme Image URL Rebasing asp.net

2.4k Views Asked by At

I am implementing themes to enable an existing website to be rebranded (logos, colors, images etc.) depending on the requesting URL. I understand how to do that and have got the skins working fine except for some exceptions related to the URLs of images.

Specifically I have a control property that it is not feasible to skin. Prior to implementing themes it looked like this:

<DisplayImageChecked Url="~/Images/BobIcon-Green.png" />

Obviously that will not work with themes. So after much trial and error and reading I am trying to implement it like this:

<DisplayImageChecked Url="~/AppThemes/<%= Page.Theme %>/Images/BobIcon-Green.png" />

However that does not work. The generated html looks like:

<img src="AppThemes/%3C%25=%20Page.Theme%20%25%3E/Images/BobIcon-Green.png"/>

Any pointers in the right direction would be appreciated.

David

4

There are 4 best solutions below

1
On

You may also use "Images/BobIcon-Green.png" as Url. ASP will take care of resolving the Url to the directory within your theme.

1
On

Is there a reason that you can't just dump the images in the same folder as the theme? If you put an image say for example: "image.gif" into the theme folder, then you can simply refer to it directly in your skin.

ImageUrl="image.gif"

This will resolve just fine when you apply this skin on a control in your page. Also much easier than trying to do the dynamic URL thing.

1
On

Use the binding syntax inside a databound control (watch the single vs double quotes):

<DisplayImageChecked Url='<%# "~/AppThemes/" + Page.Theme + "/Images/BobIcon-Green.png" %>' />
0
On

Here's the right way to go about your task:

Adorn your property with the UrlProperty attribute, this will tell ASP.NET to automatically translate your partial URL into the proper url.

Using "~/AppThemes/" + Page.Theme + "/Images/BobIcon-Green.png" will do the trick, but it's NOT the preferred way because you need to do all the work yourself and it's always good practice to leave all the work to ASP