1

We're displaying objects within a web-page which have basic height and width properties. We can generate the HTML objects utilising the HtmlTextWriter object and plan to apply the Height and Width properties by referencing the object's ID in embedded css class at the top of the class.

#bob
{
   height: 20
   width: 20
}

...

<div id="bob"></div>

1) Firstly, is this a good approach or should we be considering another approach like inline css ?

2) There doesn't appear to be anything written in the surrort of rendering css within the .net framework. Does anyone know of anything which might be of assistance in rendering css notation with the .net framework. Is there an equivalent to HtmlTextWriter ?

3) Do you know of any tools which can be of assistance in generating css from code ?

Kind Regards

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Tim
  • 298
  • 1
  • 2
  • 11
  • 3
    Keep CSS in .css, HTML in .html, C# in .cs – Greg Jun 28 '12 at 11:33
  • Hope this interests you http://www.codeproject.com/Articles/14033/Dynamic-CSS-Styling-in-ASP-NET-A-Flexible-Approach – V4Vendetta Jun 28 '12 at 11:35
  • the first question must be if the css must really be created dynamically. Means are the rules changing or every time the same, or do the ids and classnames of the object change? If not I see no reason why not to use a static external css file. – Sven Bieder Jun 28 '12 at 11:35
  • yeah sorry Sven ... probably didn't make it that clear. The CSS must be dynamically generated from the object properties. – Tim Jun 28 '12 at 13:07

3 Answers3

1

Not sure if you can do it in .net. But I am doing it in jquery as below. I hope this is someway useful.

$.rule("#bob {height:"+ht+"}").appendTo('style');

When this is generated then you can check in your dom that some css has been added to style element.

It is better than inline css. It works for the webpage which is displayed on the browser and not physically written to the file.

More info - here

It works in IE8, FF and Chrome when I checked.

Ashwin
  • 12,081
  • 22
  • 83
  • 117
0

Try almost not to use inline css and you can use a literal that has the css string in it, and let the htmlTextWriter write it.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • with css string i mean the metatag – JohnnBlade Jun 28 '12 at 11:37
  • Thanks ... found a page which explains how to link a css file at run time which is great, but I'm going to need a simple way to generate the css from the object properties. – Tim Jun 28 '12 at 12:57
0

Why are you writing out HTML, does one of the existing controls not provide what you need?

In general, CSS should be in a .css file.

It's not really possible to answer if this is a good approach since you haven't explained the intent of the code.

  • We're creating our own controls which do very specific things. I've now seen a method of programatically adding in a reference to the css file [link](http://www.devcurry.com/2009/07/how-to-add-css-programmatically-to.html) so this can be generated and linked during run time. – Tim Jun 28 '12 at 12:52