What are we facing?
I am using ASP.NET Themes and someone defined a .skin file with default visual attributes for common controls in my website. I need to change some attributes but need to preserve the default values for the rest of them.
The Solution
ASP.NET Themes are collections of appearance-related resources that will be applied to web controls on a website. Themes include images, control skins, CSS style sheets, and additional files (http://msdn.microsoft.com/en-us/library/de93t31k(VS.80).aspx).
Default attributes for ASP.NET web controls are specified in a .skin file including an entry with the same syntax as a normal web control, for example:
<asp:Label runat="server" BackColor="Red" ForeColor="Blue"></asp:Label>
Then, the .skin file can be applied to an ASP.NET page by adding the Theme attribute to the corresponding Page directive, for example:
<%@ Page Language="C#" … Theme="MySkinFile" … %>
Now, the situation is that you need to override some of the default asp:Label attributes for a specific label control. Let’s start with an example; the objective is to make the following label display with a yellow foreground color, given that the current skin file specifies that labels will be displayed with red background color and blue foreground:
<asp:Label ID="Label1" runat="server" Text="Label">This is the sample label text</asp:Label>
This objective should be achieved by changing only the specific attribute in order to keep the code maintainable, and keep redundancy low. However, this change is not as easy as it might be.
One first attempt is to change the attribute in the control instance we are interested in:
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="Yellow">This is the sample label text</asp:Label>
Unfortunately this doesn’t work; the entries in the skin file have precedence over the attributes in specific controls.
The attribute EnableTheming="false" can be used to allow the specific label control to apply different attribute values, however the rest of skin attributes, which we want to reuse, is not applied.
The SkinID attribute can be used in the .skin file entry to make it specific to a control with the same id, this option does not allow us to reuse the attributes specified in the more “generic” asp:Label skin entry.
The attributes specified in the .skin file entry will be processed by the ASP.NET server, rendering them as CSS attributes and including them in the style attribute of the applicable page elements, for example, our original asp:Label would be rendered as:
<span id="Label1" style="color:Blue;background-color:Red;">This is the sample label text </span>
This gives us the final clue to achieve our objective. A style attribute can be added to the asp:Label control in order to override the default attributes provided by the .skin file. The source code would be:
<asp:Label ID="Label1" runat="server" Text="Label" Style="color:yellow;">This is the sample label text</asp:Label>
This would be finally rendered as:
<span id="Label1" style="color:Blue;background-color:Red;color:yellow;">This is the sample label text</span>
Some external references
[advertisement] Try
Aggiorno for free, the assistant tool for web development you always wanted.