views:

237

answers:

3

Is there a way to apply a skin only to controls of a certain type and a specific ID? For instance, I have controls throughout a site used to display our company phone number that look like this:

<asp:Label ID="PhoneLabel" runat="server"></asp:Label>

I'm wondering if I can set the theme programmatically and use a skin for that theme to set the text property of the label.

I know I can do this

<asp:Label runat="server" Text="319-867-5309"></asp:Label>

but that will set the text for all the labels on the site. I only want to set the text for labels with an ID of PhoneLabel.

I'm also aware of the SkinID property but that seems to tie my control to a specific skin and will not allow me to change the phone number text by applying another theme.

Is it just not possible to do this with themes/skins?

A: 

You cannot do this with themes and skins as far as I know. Properties that are not Theme related Id, Text, and etc cannot be set. You can set only those properties that have a ThemeableAttribute attribute set to true in the control class.

http://msdn.microsoft.com/en-us/library/ykzx33wh.aspx

ElvisLives
+1  A: 

Yes, it is possible to set the text using Skin files and to have different text in different Themes.

Let's say you have two themes in your project: "Theme1" and "Theme2". In each theme you have a file called "Default.skin".

In your Theme1/Default.skin file you set

<asp:Label runat="server" SkinID="PhoneLabel" Text="319-867-5309"></asp:Label>

In your Theme2/Default.skin file you set

<asp:Label runat="server" SkinID="PhoneLabel" Text="555-555-5555"></asp:Label>

In your aspx file you set

<asp:Label ID="PhoneLabel" SkinID="PhoneLabel" runat="server"></asp:Label>

Either in your web.config or aspx page you set the styleSheetTheme to the "Theme1" if you want the first number and "Theme2" if you want the second number.

--

Now that I've answered the question, I'd like to suggest that Skins/Themes might not be the best way to do this. There are other solutions such as making a custom Phone Number control that pulls the phone number from an underlying data source or using a resource file.

Greg
+3  A: 

If you simply want to display the phone number (and want to be able to change it globally), then the following two approaches might be easier:


Read the phone number from the web.config file, e.g:

<asp:Label runat="server" Text="<%$ AppSettings:PhoneNumber %>"/>

and put the phone number into the appSettings section of your web.config:

<appSettings>
  <add key="phoneNumber" value="12344"/>


Derive a class from Label, and inside that class set the Text property to the phone number (either hard-coded, or reading from config, etc). Then whenever you want to display the phone number, use that control instead of the standard Label control, e.g:

put this in App_Code:

namespace MyControls
{
    public class PhoneNumberLabel : Label
    {
        public override string Text
        {
            get { return "123456"; }
            set { base.Text = value; }
        }
    }
}

Then use that control to display the phone number:

<%@ Register Assembly="App_Code" Namespace="MyControls" TagPrefix="my" %>
....
<my:PhoneNumberLabel runat="server"></my:PhoneNumberLabel>
M4N