20

Based on a simple test I ran, I don't think it's possible to put an inline <style> tag into an ASP.NET server control. The style did not end up rendering to the output HTML. Even if it was possible, I'm sure it is bad practice to do this.

Is it possible to do this? I can see it being useful for quick prototypes that just have 1 or 2 CSS classes to apply.

1
  • Could you provide a code example of what you tried to do and how you expected it to render in the HTML? That'd help us solve your problem. Commented Sep 18, 2008 at 13:16

4 Answers 4

58

Intellisense won't give you hints but you can do this:

<asp:Label ID="Label1" runat="server" Text="Label" style="color:Red;"></asp:Label>
Sign up to request clarification or add additional context in comments.

Comments

14

According to www.w3schools.com:

The style element goes in the head section. If you want to include a style sheet in your page, you should define the style sheet externally, and link to it using <link>.

So it's not a good idea to include style elements (e.g. a <style type="text\css"></style> block) in a control. If you could, it'd probably have an effect in some browsers but it wouldn't validate and is bad practice.

If you want to apply styles inline to an element then either of these would work:

C#

myControl.Attributes["style"] = "color:red";

myControl.Attributes.Add("style", "color:red");

VB.NET

myControl.Attributes("style") = "color:red";

myControl.Attributes.Add("style", "color:red");

But bear in mind that this will replace any existing styles that are set on the style attribute. This may be a problem if you try setting styles in more than one place in the code so is something to watch out for.

Using CSS classes would be preferable as you can group multiple style declarations and avoid redundancy and page bloat. All controls derived from WebControl have a CssClass property which you can use, but again be careful not to overwrite existing classes that have been applied elsewhere.

Comments

7

If you use Attributes["style"], you are overwriting the style each time you call it. This can be an issue if you are making the call in two different sections of code. As well, it can be an issue because the framework includes properties for basic settings like border and colour that also will be applied as inline styles. Here is an example:

// dangerous: first style will be overwritten
myControl.Attributes["style"] = "text-align:center";
// in some other section of code
myControl.Attributes["style"] = "width:100%";

To play nicely, set styles like this instead:

// correct: both style settings are applied
myControl.Attributes.CssStyle.Add("text-align", "center");
// in some other section of code
myControl.Attributes.CssStyle.Add("width", "100%");

1 Comment

@TylerH - The first code snippet in my answer, which I explain can be problematic, appears in other answers. The second answer, which I suggest is the preferable approach, does not.
1

I think you will have to add it as an attribute to the server control... for it to render to HTML.

So basically (in C#),

ControlName.Attributes["style"] = "color:red";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.