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.
According to http://www.w3schools.com:
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#
VB.NET
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.