473,594 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add a StyleSheet to the body dynamically

I use the code below to change to a style sheet that has:

body

{
....
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

....

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.Att ributes.Add("hr ef", "StyleSheet s/Textured.css")

HtmlLinkObj.Att ributes.Add("re l", "stylesheet ")

HtmlLinkObj.Att ributes.Add("ty pe", "text/css")

HeadMaster.Cont rols.Add(HtmlLi nkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head (I
think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks




Sep 18 '08 #1
6 2544
Use the following code:

this.Page.Heade r.StyleSheet.Cr eateStyleRule(n ew
CustomStyle("ba ckground-color:black;"), null, "body");

The two important parameters in this method are the first one, which is a
Style object, and the third one, which is the selector to be used with the
style rule. You will also need to include the following class:

public class CustomStyle : System.Web.UI.W ebControls.Styl e
{
private System.Web.UI.C ssStyleCollecti on currstyles;

public CustomStyle(Sys tem.Web.UI.CssS tyleCollection custom) {
this.currstyles = custom; }
public CustomStyle(str ing cssvalue)
{
System.Web.UI.C ssStyleCollecti on tempstyle = new
System.Web.UI.W ebControls.WebC ontrol(System.W eb.UI.HtmlTextW riterTag.Unknow n).Style;
tempstyle.Clear ();
tempstyle.Value = cssvalue;
this.currstyles = tempstyle;
}

protected override void
FillStyleAttrib utes(System.Web .UI.CssStyleCol lection
attributes,Syst em.Web.UI.IUrlR esolutionServic e urlResolver)
{
base.FillStyleA ttributes(attri butes, urlResolver);
foreach (string currkey in this.currstyles .Keys) { attributes[currkey] =
this.currstyles[currkey]; }
}
}

It took me a while, and some help as well, to figure out how to do this, but
I now use this class quite often, it can be quite useful when dynamically
determining an attribute that is used in many elements. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez******** ********@TK2MSF TNGP04.phx.gbl. ..
>I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.Att ributes.Add("hr ef", "StyleSheet s/Textured.css")

HtmlLinkObj.Att ributes.Add("re l", "stylesheet ")

HtmlLinkObj.Att ributes.Add("ty pe", "text/css")

HeadMaster.Cont rols.Add(HtmlLi nkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head (I
think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks




Sep 18 '08 #2
I'll bet it did take some time.
Thanks for sharing.
I need to change the entire style sheet file.
I'm not sure but I think this code is for changing one rule at a time.
Is that correct?

Thanks again

"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Use the following code:

this.Page.Heade r.StyleSheet.Cr eateStyleRule(n ew
CustomStyle("ba ckground-color:black;"), null, "body");

The two important parameters in this method are the first one, which is a
Style object, and the third one, which is the selector to be used with the
style rule. You will also need to include the following class:

public class CustomStyle : System.Web.UI.W ebControls.Styl e
{
private System.Web.UI.C ssStyleCollecti on currstyles;

public CustomStyle(Sys tem.Web.UI.CssS tyleCollection custom) {
this.currstyles = custom; }
public CustomStyle(str ing cssvalue)
{
System.Web.UI.C ssStyleCollecti on tempstyle = new
System.Web.UI.W ebControls.WebC ontrol(System.W eb.UI.HtmlTextW riterTag.Unknow n).Style;
tempstyle.Clear ();
tempstyle.Value = cssvalue;
this.currstyles = tempstyle;
}

protected override void
FillStyleAttrib utes(System.Web .UI.CssStyleCol lection
attributes,Syst em.Web.UI.IUrlR esolutionServic e urlResolver)
{
base.FillStyleA ttributes(attri butes, urlResolver);
foreach (string currkey in this.currstyles .Keys) { attributes[currkey] =
this.currstyles[currkey]; }
}
}

It took me a while, and some help as well, to figure out how to do this,
but I now use this class quite often, it can be quite useful when
dynamically determining an attribute that is used in many elements. Good
Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez******** ********@TK2MSF TNGP04.phx.gbl. ..
>>I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.At tributes.Add("h ref", "StyleSheet s/Textured.css")

HtmlLinkObj.At tributes.Add("r el", "stylesheet ")

HtmlLinkObj.At tributes.Add("t ype", "text/css")

HeadMaster.Con trols.Add(HtmlL inkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head
(I think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks





Sep 18 '08 #3
That is correct. If you need to dynamically change an entire stylesheet at
the same time, you can do one of the following:

1. Call the this.Page.Heade r.StyleSheet.Cr eateStyleRule method multiple
times

2. Create an *.aspx file that returns ContentType="te xt/css" and use your
original method to set the href attribute to that file, possibly with a
querystring

Are the different style rules that you will have generated or just
different? If they are generated, then you really don't have much choice
other than my suggestions. If they are just different, then you could use
the following technique instead:

1. Create several *.css files
2. Add id and runat="server" attributes to the link tag
3. In your code, set the href attribute of the link tag to the location of
the appropriate *.css file

Also, if there are only a few style rules that need to be determined in
code, use a stylesheet like you normally would for those to keep the code to
a minimum. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
I'll bet it did take some time.
Thanks for sharing.
I need to change the entire style sheet file.
I'm not sure but I think this code is for changing one rule at a time.
Is that correct?

Thanks again

"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Use the following code:

this.Page.Head er.StyleSheet.C reateStyleRule( new
CustomStyle("b ackground-color:black;"), null, "body");

The two important parameters in this method are the first one, which is a
Style object, and the third one, which is the selector to be used with
the style rule. You will also need to include the following class:

public class CustomStyle : System.Web.UI.W ebControls.Styl e
{
private System.Web.UI.C ssStyleCollecti on currstyles;

public CustomStyle(Sys tem.Web.UI.CssS tyleCollection custom) {
this.currstyle s = custom; }
public CustomStyle(str ing cssvalue)
{
System.Web.UI.C ssStyleCollecti on tempstyle = new
System.Web.UI. WebControls.Web Control(System. Web.UI.HtmlText WriterTag.Unkno wn).Style;
tempstyle.Clear ();
tempstyle.Value = cssvalue;
this.currstyles = tempstyle;
}

protected override void
FillStyleAttri butes(System.We b.UI.CssStyleCo llection
attributes,Sys tem.Web.UI.IUrl ResolutionServi ce urlResolver)
{
base.FillStyleA ttributes(attri butes, urlResolver);
foreach (string currkey in this.currstyles .Keys) { attributes[currkey]
= this.currstyles[currkey]; }
}
}

It took me a while, and some help as well, to figure out how to do this,
but I now use this class quite often, it can be quite useful when
dynamically determining an attribute that is used in many elements. Good
Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez******* *********@TK2MS FTNGP04.phx.gbl ...
>>>I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.A ttributes.Add(" href", "StyleSheet s/Textured.css")

HtmlLinkObj.A ttributes.Add(" rel", "stylesheet ")

HtmlLinkObj.A ttributes.Add(" type", "text/css")

HeadMaster.Co ntrols.Add(Html LinkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head
(I think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks






Sep 18 '08 #4
So my code down below builds a new link and adds it to the master's
controls. You're saying that instead I can just change the href on the
existing one. Is that right?

I did what you said which replaced the 5 lines below with one line. I show
it below in case anyone else reads this thread. There are many sites in the
Internet that suggest the way I had done it. This seems simpler.

StyleSheetLink. Href = "StyleSheet s/Textured.css"

The only thing is that I had to make the link run at server. Is there a down
side to that?

Thanks a lot

"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:uY******** ******@TK2MSFTN GP04.phx.gbl...
That is correct. If you need to dynamically change an entire stylesheet at
the same time, you can do one of the following:

1. Call the this.Page.Heade r.StyleSheet.Cr eateStyleRule method multiple
times

2. Create an *.aspx file that returns ContentType="te xt/css" and use your
original method to set the href attribute to that file, possibly with a
querystring

Are the different style rules that you will have generated or just
different? If they are generated, then you really don't have much choice
other than my suggestions. If they are just different, then you could use
the following technique instead:

1. Create several *.css files
2. Add id and runat="server" attributes to the link tag
3. In your code, set the href attribute of the link tag to the location of
the appropriate *.css file

Also, if there are only a few style rules that need to be determined in
code, use a stylesheet like you normally would for those to keep the code
to a minimum. Good Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
>I'll bet it did take some time.
Thanks for sharing.
I need to change the entire style sheet file.
I'm not sure but I think this code is for changing one rule at a time.
Is that correct?

Thanks again

"Nathan Sokalski" <nj********@hot mail.comwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
>>Use the following code:

this.Page.Hea der.StyleSheet. CreateStyleRule (new
CustomStyle(" background-color:black;"), null, "body");

The two important parameters in this method are the first one, which is
a Style object, and the third one, which is the selector to be used with
the style rule. You will also need to include the following class:

public class CustomStyle : System.Web.UI.W ebControls.Styl e
{
private System.Web.UI.C ssStyleCollecti on currstyles;

public CustomStyle(Sys tem.Web.UI.CssS tyleCollection custom) {
this.currstyl es = custom; }
public CustomStyle(str ing cssvalue)
{
System.Web.UI.C ssStyleCollecti on tempstyle = new
System.Web.UI .WebControls.We bControl(System .Web.UI.HtmlTex tWriterTag.Unkn own).Style;
tempstyle.Clear ();
tempstyle.Value = cssvalue;
this.currstyles = tempstyle;
}

protected override void
FillStyleAttr ibutes(System.W eb.UI.CssStyleC ollection
attributes,Sy stem.Web.UI.IUr lResolutionServ ice urlResolver)
{
base.FillStyleA ttributes(attri butes, urlResolver);
foreach (string currkey in this.currstyles .Keys) { attributes[currkey]
= this.currstyles[currkey]; }
}
}

It took me a while, and some help as well, to figure out how to do this,
but I now use this class quite often, it can be quite useful when
dynamically determining an attribute that is used in many elements. Good
Luck!
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/

"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez****** **********@TK2M SFTNGP04.phx.gb l...
I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj. Attributes.Add( "href", "StyleSheet s/Textured.css")

HtmlLinkObj. Attributes.Add( "rel", "stylesheet ")

HtmlLinkObj. Attributes.Add( "type", "text/css")

HeadMaster.C ontrols.Add(Htm lLinkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head
(I think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks








Sep 18 '08 #5
In addition to Nathan's suggestion, you can dynamically alter the entire CSS
(point to a different file). There are two methods to do this:

1. If all you are changing is CSS, try this:

<link id="MyStyleShee t" rel="stylesheet " type="text/css" runat="server" />

You leave the actual pointer empty here. You can then do the following:

MyStyleSheet.At tributes.Add("h ref", "{location of file}");

You can also do an Attributes.Remo ve() first if the CSS is only altered on
one or two pages, as it simplifies your code model and allows you to supply
a default.

2. If you envision the possibility of bigger changes (particular skins,
etc.), consider themes instead. You create different themes and you can then
dynamically change them in code based on a user's choices. There are plenty
of examples of this on the web.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez******** ********@TK2MSF TNGP04.phx.gbl. ..
>I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.Att ributes.Add("hr ef", "StyleSheet s/Textured.css")

HtmlLinkObj.Att ributes.Add("re l", "stylesheet ")

HtmlLinkObj.Att ributes.Add("ty pe", "text/css")

HeadMaster.Cont rols.Add(HtmlLi nkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head (I
think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks



Sep 18 '08 #6
Thanks, now I see the general approach.


"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMwrote in
message news:e2******** *****@TK2MSFTNG P03.phx.gbl...
In addition to Nathan's suggestion, you can dynamically alter the entire
CSS (point to a different file). There are two methods to do this:

1. If all you are changing is CSS, try this:

<link id="MyStyleShee t" rel="stylesheet " type="text/css" runat="server" />

You leave the actual pointer empty here. You can then do the following:

MyStyleSheet.At tributes.Add("h ref", "{location of file}");

You can also do an Attributes.Remo ve() first if the CSS is only altered on
one or two pages, as it simplifies your code model and allows you to
supply a default.

2. If you envision the possibility of bigger changes (particular skins,
etc.), consider themes instead. You create different themes and you can
then dynamically change them in code based on a user's choices. There are
plenty of examples of this on the web.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"_Who" <Ca**********@r oadrunner.comwr ote in message
news:ez******** ********@TK2MSF TNGP04.phx.gbl. ..
>>I use the code below to change to a style sheet that has:

body

{
...
background-image:url(../images/background brown.gif);

}

Rather than:

body

{

...

background-color:black;

}

Dim HtmlLinkObj As HtmlLink = New HtmlLink()

HtmlLinkObj.At tributes.Add("h ref", "StyleSheet s/Textured.css")

HtmlLinkObj.At tributes.Add("r el", "stylesheet ")

HtmlLinkObj.At tributes.Add("t ype", "text/css")

HeadMaster.Con trols.Add(HtmlL inkObj)

Works OK on IE but has no effect in FireFox nor Safari

On the .master there is:

<head id="HeadMaster" ...

I once saw where some one added to the body tag in addition to the head
(I think).

Been looking but can't find it now that I'd like to try it.

Do you know how to do that?

Have any other ideas how to make it work in FireFox and Safari

Thanks




Sep 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2865
by: wjbell | last post by:
I have a piece of javascript I need to modify. Right now it changes a stylesheet in the document between style.css and no_indent.css. These are in the head of my document: <link rel=stylesheet href=/style.css> <link rel=stylesheet href=/no_indent.css> <link rel=stylesheet href=/style.css> What the code below does is toggle between the two depending on what
0
1625
by: Catherine Lynn Wood | last post by:
I have a page that I just developed using a combination of stylesheets and div layers. It uses a 'tab' style system placing four div layers in the same space with visibility 'hidden' and position absolute. They are inside of another parent div container with position set relative so that as I make the objects visible, I can also set their position to relative so they redraw the container size. The parent div is inside of a table cell...
3
1797
by: Mutkey | last post by:
Hi; I have 2 sides of my website, which uses 2 different stylesheets.They have in common a feedback (HTML02) page. Currently I have duplicated the feedback pages in both sides of the webpage so they show the correct stylesheet for that area. But this involves double updating everytime the feedback is changed. How could I have HTML01 file pointing to the feedback (HTML02) page,
3
4323
by: Jamie | last post by:
Hi, Thanks for the excellent answer to my last question! One more: Does anyone have a method they follow for organizing stylesheets themselves? They seem like they can get bloated and hard to read. Aside from putting all the "h1" rules together, I haven't thought of any way to do it, if it's necessary at all. J.
7
2697
by: Vincent van Beveren | last post by:
Hi everyone I have a JavaScript app that creates an IFRAME through DOM (createElement('IFRAME')) However, that IFRAME does not have any content yet. alert(iframe.contentWindow.document.documentElement) gives null.
3
11912
by: Dave | last post by:
Is there a way to dynamically add a reference to the css stylesheet from the codebehind similarly to the script registration features? I was thinking of adding this code to a base class and inherit all my pages from it so the css link below is added to each page automatically in the <head> section.... <LINK href="/MyApp/Css/MyApp.css" type="text/css" rel="stylesheet"> Thanks, Dave.
3
5504
by: juergen.riemer | last post by:
Hi all, I have a problem in attaching an external stylesheet to a newly created iframe in Firefox (1.5.0.2). If I use the code below (with a relative address to the file) the stylesheet would not be applied. I have to indicate the absolute path: var uriStylesheet = "http://localhost:8080/panel/styles.css"; to make it work in Firefox. NB: if I try to attach the external stylesheet to the topmost body (the
3
5157
by: 2good2b | last post by:
Is there a way to change the background color dynamically, when the color is defined at a CSS stylesheet? (tried document.bgcolor and this.style.background-color) Thanks
1
3183
by: msarora | last post by:
I'm using SimpleTransform.java (renamed as ReceiptTransformer.java for custom use) found in xalan-j_2_7_0 samples directory for transformations in my custom application. The program compiles successfully but gives the following error during runtime: Below is the copy of the stylesheet. I'll really appreciate any help or clue in this regard.
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.