Dynamically add the stylesheet(.CSS file)

  • Thread starter Thread starter VSK
  • Start date Start date
V

VSK

Hi,

Iam looking for a way to dynamically add the stylesheet.

I have all the .aspx pages extend from a CommonPage.cs which extend from
System.Web.UI.Page.

Now i want a way to detect the browser(netsacpe/ie) and load the curr .css
file in CommonPage so that it applies to all the pages which extend from
commonpage.

Is there a way i can do.

any inputs will be helpful

Thx
VSK
 
VSK,

I think the easiest way would be to turn the style tag into a server side
control.

Set your style tag to look like this (with the href set to a default style
sheet):

<LINK id="StyleTag" runat="server" rel="stylesheet" type="text/css"
href="http://localhost/lab/styles.css">

Please note that I've added the attributes: id and runat to the tag.

Now in the code-behind page declare a matching object for the "StyleTag" id
like this:

Protected StyleTag As System.Web.UI.HtmlControls.HtmlGenericControl

Now you can use the StyleTag object to set any attribute from the
code-behind page.

In order to do what you want (change the href) you would use code like this:

StyleTag.Attributes.Add("href", "http://localhost/lab/NewStyles.css")


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Thanks a lot!

S. Justin Gengo said:
VSK,

I think the easiest way would be to turn the style tag into a server side
control.

Set your style tag to look like this (with the href set to a default style
sheet):

<LINK id="StyleTag" runat="server" rel="stylesheet" type="text/css"
href="http://localhost/lab/styles.css">

Please note that I've added the attributes: id and runat to the tag.

Now in the code-behind page declare a matching object for the "StyleTag" id
like this:

Protected StyleTag As System.Web.UI.HtmlControls.HtmlGenericControl

Now you can use the StyleTag object to set any attribute from the
code-behind page.

In order to do what you want (change the href) you would use code like this:

StyleTag.Attributes.Add("href", "http://localhost/lab/NewStyles.css")


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Back
Top