How to register style

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
 
G

Guest

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
 
A

Alan Silver

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
 
M

marss

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
 
M

marss

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
 
C

Cubaman

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
 
M

marss

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top