473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Adding Style at run time

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
Aug 25 '06 #1
8 2457
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","Stylesheetnam e.css")
End If
End Sub

--
Regards

John Timney (MVP)

"Ganesh Muthuvelu" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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

Aug 25 '06 #2
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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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

Aug 25 '06 #3
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 [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:ep**************@TK2MSFTNGP03.phx.gbl...
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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
>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


Aug 25 '06 #4
Thanks John,
But now how would I add the "link" tag at run time?.

Thanks,
Ganesh

"John Timney (MVP)" wrote:
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","Stylesheetnam e.css")
End If
End Sub

--
Regards

John Timney (MVP)

"Ganesh Muthuvelu" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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


Aug 25 '06 #5
Mine doesn't work?

"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.comwro te in message
news:eB**************@TK2MSFTNGP06.phx.gbl...
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 [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:ep**************@TK2MSFTNGP03.phx.gbl...
>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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
>>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



Aug 25 '06 #6
PERFECT!!! KEN, THANKS A BUNCH!

"Ken Cox [Microsoft MVP]" wrote:
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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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


Aug 25 '06 #7
Hi Ganesh,

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

http://msdn2.microsoft.com/en-us/lib...stylerule.aspx

"Ganesh Muthuvelu" <Ga*************@discussions.microsoft.comwrote in
message news:E0**********************************@microsof t.com...
PERFECT!!! KEN, THANKS A BUNCH!

"Ken Cox [Microsoft MVP]" wrote:
>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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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



Aug 26 '06 #8
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 [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
Mine doesn't work?

"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.comwro te in message
news:eB**************@TK2MSFTNGP06.phx.gbl...
>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 [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in
message news:ep**************@TK2MSFTNGP03.phx.gbl...
>>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" <Ga*************@discussions.microsoft.comwrote in
message news:37**********************************@microsof t.com...
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




Aug 26 '06 #9

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

Similar topics

4
by: Max Derkachev | last post by:
Good day to all. Some time ago I'd been playing with a framework which uses dynamic class creation havily. Say, I could do: class A: pass # I method name is dynamic meth_name = 'foo'
10
by: xxbmichae1 | last post by:
I'm having a hard time getting this code to work...the onMouseOver and onMouseOut events don't seem to be firing in IE or Mozilla.....thanks in advance for any assistance... var imgf =...
32
by: Ed Jay | last post by:
A complete js newbie is asking this question: I have a form comprised of several questions, each answered with a radio button. I'd like to use onClick to bring up additional text at the end of...
3
by: The Crow | last post by:
how does it affect performance and memory consumption? i think there will be no difference memory consumption per instance.
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
11
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.