Nol Nol - 10 months ago 40
ASP.NET (C#) Question

Change the size of all asp:Label Text

I have a LOT of

s and I want to change the size of the font for every single one.

I know that they are converted to
but I have other elements with text that get converted to spans which I don't want to change.

So how can I apply style to only the the spans generated from Labels all at once?

Please don't tell me I'll have to go through each one and apply a cssClass directly.

Answer Source

You can define a Skin for that. Think of Skin as a set of settings common for all controls of specific type. Skins do not exist on their own, but within a theme. Here is a walkthrough on how to create a theme from MSDN. I do not think it is reasonable to copy it here.

But to focus on your specific use case, go ahead and follow this walkthrough and create a theme with only a single skin defined like so:

<asp:Label runat="server" CssClass="CommonCssClass" />

Notice we do not use SkinID, so this will be applied to all Label controls.

Next go to the page and modify Page directive:

<%@ Page Theme="YourThemeName" %>

That's it, this should apply skin defined in the theme to the labels on the page.

In fact I would make an argument that this is a better way to do similar changes to server side controls then trying to figure out how to capture resulting HTML with css selector. The latter puts your code into dependency on the generated markup, which is dangerous as ASP.NET does not always generate what you would expect. For example, Label does not always render as span.