Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading stylesheets at runtime

Status
Not open for further replies.

rotsey

Programmer
Nov 23, 2004
135
0
0
AU
Hi,

Is it possible to load stylesheets for a page at runtime in .NET????

I am thinking about giving the user an option of styles.

rotsey
 
Yes it is. As it is possibly in HTML then it is possible in ASP.NET (after all ASP.NET really just provides rendered HTML to the browser).

Have a look for examples on how to do it in HTML and follow those.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Rotsey, here is an example I did using javascript. I changed the stylesheet as well as logo.

Code:
<script language="javascript">

	document.write('<LINK href="Styles.css" type="text/css" rel="stylesheet">');
	
	function changeStyle(){
	
		if (document.getElementsByTagName("LINK")(0).href == "Styles.css"){
		document.getElementsByTagName("LINK")(0).href = "Styles2.css";
		document.getElementsByTagName("IMG")(0).src = "images/yellow_dawn.jpg";
		}
		else{
		document.getElementsByTagName("LINK")(0).href = "Styles.css";
		document.getElementsByTagName("IMG")(0).src = "images/logo.jpg";
			}
		}
</script>


<table align="center">
	<tr>
	    <td>
		<a href="javascript:changeStyle(newss)">ChangeSheet</a>
	    </td>
	</tr>
</table>
 
Hi,

You can also do it with an asp literal.

<HEAD>
<asp:literal id=Literal1 runat="server"> </asp:literal>
</HEAD>

Now you can manipulate it in your codebehind:

Literal1.Text = "<LINK href='css/Style1.css' type=text/css rel=stylesheet>"

Note the single quotes around the path to the file.

JB

----------------------------------------------------------------------------------------
Try saying Tek-Tips Tek-Tips Tek-Tips more than 3 times in a row....no chance!
 
sirjon,

I tried doing it using the ASP Literal, but my codebehind tells me that it cannot be found. This is what I have in the HTML of header.ascx:

Code:
<asp:Literal id="cssLink" runat="server"></asp:Literal>

Then I have the following in the header.ascx.cs codebehind:

Code:
cssLink.Text = "<link type='text/css' href='css/ie.css' rel='stylesheet'>";

The compiler tells me that "The type or namespace 'cssLink' could not be found."

Any ideas?
 
Is the 'cssLink' variable properly declared in your header.ascx.cs? You should see something like this in your variable declaration.

private System.Web.UI.Controls.Literal cssLink;
 
This will work..



YOUR ASPX PAGE:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="MyNameSpace.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<asp:placeHolder id="TitleHolder1" runat="server" />
<asp:placeHolder id="Styleholder1" runat="server" />
</HEAD>
<body >


YOUR CODE BEHIND:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TitleHolder1.Controls.AddAt(0, LoadControl("header.ascx"))
Styleholder1.Controls.AddAt(0, LoadControl("style.ascx"))
End Sub


YOUR STYLE CONTROL:

<%@ Control %>
<style>
.red { FONT-SIZE: 10px; COLOR: red }
.black { FONT-SIZE: 13px; COLOR: black }
</style>


YOUR TITLE CONTROL:

<%@ Control %>
<title>My New Title</title>


Keeping it Simple
 
This will work also with the above code:


Dim lit As New Literal
lit.Text = "<style>.red { FONT-SIZE: 10px; COLOR: red }</style>"
Styleholder1.Controls.Add(lit)

Keeping it Simple
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top