How to register style

  • Thread starter Thread starter marss
  • Start date Start date
M

marss

I know that I can register a few predefined style attributes in such
way:
Style newStyle = new Style();
newStyle.ForeColor = System.Drawing.Color.Red;
Page.Header.StyleSheet.CreateStyleRule(newStyle, null, ".text");

The result of above code execution is an additional style in the head
section of the HTML document.
<style type="text/css">
..text { color:Red; }
</style>

Is there any standard way to register, for example, following style?
<style type="text/css">
..text { padding:5px 2px 10px 8px; }
</style>

I think about
Page.Header.Controls.Add(new LiteralControl(@"<style type='text/
css'> .text { padding:5px 2px 10px 8px; } </style>"));

Does a better/more standard/simpler solution exist?

Mykola
http://marss.co.ua
 
Hi,

do you work with a MasterPage? An alternative way would be to define a
ContentPlaceHolder in the Header and fill it with the stylesheet data.

hth
 
Does a better/more standard/simpler solution exist?

Sure, use a regular CSS file! Why are you doing it programmatically?
Unless there is some pressing need for this, you would be far better
putting the styles in a CSS file (where they belong) and either adding a
<link> tag in your <head>, or using themes, which will add any CSS files
for you.

HTH
 
Hi,

do you work with a MasterPage? An alternative way would be to define a
ContentPlaceHolder in the Header and fill it with the stylesheet data.

hth

Thank Andreas,
Using ContentPlaceHolder is no more standard way than using of
LiteralControl. :)
I am looking for something like
Page.ClientScript.RegisterClientScriptBlock.
But for styles, of course.

Mykola
http://marss.co.ua
 
Sure, use a regular CSS file! Why are you doing it programmatically?
Unless there is some pressing need for this, you would be far better
putting the styles in a CSS file (where they belong) and either adding a
<link> tag in your <head>, or using themes, which will add any CSS files
for you.

HTH

Hi Alan,
Thank for answer. Styles are generated in runtime in my code depending
on some settings.
I am interesting about existing solutions before I'll start "to invent
own wheel" :)

Mykola
http://marss.co.ua
 
Hi Alan,
Thank for answer. Styles are generated in runtime in my code depending
on some settings.
I am interesting about existing solutions before I'll start "to invent
own wheel" :)

Mykolahttp://marss.co.ua

Setting styles depending on user preferences is exactly what themes
do. And very easy to implement.
Best regards
Oscar Acosta
 
Setting styles depending on user preferences is exactly what themes
do. And very easy to implement.
Best regards
Oscar Acosta

Thanks, Oscar
As far as I know themes are defined in design time and can be changed
according to user settings, but themes can't be generated in code in
runtime.
I have written a class to register stylesheet in runtime. I also wrote
an article on this topic. If you are interested you may find it here:
http://marss.co.ua/StylesheetRegistration.aspx

Mykola
 
Back
Top