ASP.NET Themed Themes

Is there a way to only apply a skin to controls of a specific type and specific ID? For example, I have controls on a site used to display our company number that looks like this:

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

      

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

I know i can do it

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

      

but this will set the text for all tags on the site. I only want to set the text for the labels with the ID PhoneLabel.

I also know the SkinID property, but it seems to bind my control to a specific skin and won't let me change the text of the phone number by applying another theme.

Isn't it possible to do this with themes / skins?

+2


source to share


3 answers


Yes, it is possible to set text using Skin files and 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 the web.config page or in aspx you set styleSheetTheme to "Theme1" if you want the first number and "Theme2" if you want the second number.

-

Now that I have answered this question, I would like to suggest that Skins / Themes is not the best way to do this. There are other solutions, such as creating a custom Phone Number element that pulls a phone number from the underlying data source or uses a resource file.

+1


source


If you just 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, for example:

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

      

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

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

      




Take the class out of Label and inside this class set the Text property to the phone number (either hardcoded or read from config, etc.). Then, when you want to display a phone number, use that control instead of the standard Label control, for example:

put this in App_Code:

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

      

Then use this control to display the phone number:

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

      

+3


source


You cannot do this with themes and skins as far as I know. Properties that are not related to the tag specified in the text, text, etc. cannot be set. You can only set properties that have the ThemeableAttribute set to true in the control class.

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

0


source







All Articles