Adding Style at run time

G

Guest

Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 
J

John Timney \(MVP\)

You could declare the link id for the stylesheet with a runat sever tag and
use an external stylesheet

<head>
<link id="cssStyleSheet" rel="stylesheet" type="text/css" runat="server" />
</head>
Sub Page_Load(Sender As Object, E As EventArgs)
If Not (IsPostBack)
cssStyleSheet.Attributes.Add("href","Stylesheetname.css")
End If
End Sub
 
K

Ken Cox [Microsoft MVP]

Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server" text="Style
created in code"></asp:label></div>
</form>
</body>
</html>
 
C

clintonG

Check out the new classes related to the head element buy using this search
term...

HtmlHead class site:msdn2.microsoft.com

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W

Ken Cox said:
Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server"
text="Style created in code"></asp:label></div>
</form>
</body>
</html>


Ganesh Muthuvelu said:
Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 
G

Guest

PERFECT!!! KEN, THANKS A BUNCH!

Ken Cox said:
Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server" text="Style
created in code"></asp:label></div>
</form>
</body>
</html>


Ganesh Muthuvelu said:
Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 
K

Ken Cox [Microsoft MVP]

Mine doesn't work?

clintonG said:
Check out the new classes related to the head element buy using this
search term...

HtmlHead class site:msdn2.microsoft.com

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W

Ken Cox said:
Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server"
text="Style created in code"></asp:label></div>
</form>
</body>
</html>


Ganesh Muthuvelu said:
Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run
time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 
K

Ken Cox [Microsoft MVP]

Hi Ganesh,

You're welcome, and here's a further reference that I just found:

http://msdn2.microsoft.com/en-us/library/system.web.ui.istylesheet.createstylerule.aspx

Ganesh Muthuvelu said:
PERFECT!!! KEN, THANKS A BUNCH!

Ken Cox said:
Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server"
text="Style
created in code"></asp:label></div>
</form>
</body>
</html>


Ganesh Muthuvelu said:
Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run
time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 
C

clintonG

Sure, that code snippet works fine Ken but the context of the head element
is much broader than a snippet infers so I thought I'd throw in the big
picture in case the OP is a serious developer who may be currently ignorant
but wants to learn how to master the framework. Once finding and loading the
documentation for a class such as the HtmlHead class the menu for MSDN
documentation will also display all the related classes such as HtmlLink,
HtmlMeta and so on. Very insightful wouldn't you agree?

As you may recall, one of the most frequently asked questions used to be
"[should I | how do I] do this using my own code or is there a class already
built in the framework?" That was early in the days when those who really
wanted to learn to be masterful were interested in delving into the
framework.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W



Ken Cox said:
Mine doesn't work?

clintonG said:
Check out the new classes related to the head element buy using this
search term...

HtmlHead class site:msdn2.microsoft.com

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W

Ken Cox said:
Hi Ganesh,

Here's some code that creates a style in code at runtime.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Instantiate a style object
Dim styl As New Style
' Set the style (e.g., background colour)
styl.BackColor = Drawing.Color.Aqua
' Assign the style, URL [nothing here] and selector
Page.Header.StyleSheet.CreateStyleRule _
(styl, Nothing, ".MyClass")
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="header1" runat="server">
<title>Creating a style at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label cssclass="MyClass" id="Label1" runat="server"
text="Style created in code"></asp:label></div>
</form>
</body>
</html>


message Hello,
I am a ASPX page where I have defined the style for the head element.
******************
<head runat="server" >
<style type="text/css">
.MyClass
{
background-color: aqua;
}
</style>
</head>
******************

However, instead of doing at design time, I need to do this at run
time..
Can someone help how to add this @ run time to the page?.

Thanks,
Ganesh
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top