PC Review


Reply
Thread Tools Rate Thread

Adding Style at run time

 
 
=?Utf-8?B?R2FuZXNoIE11dGh1dmVsdQ==?=
Guest
Posts: n/a
 
      25th Aug 2006
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
 
Reply With Quote
 
 
 
 
John Timney \(MVP\)
Guest
Posts: n/a
 
      25th Aug 2006
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

--
Regards

John Timney (MVP)



"Ganesh Muthuvelu" <(E-Mail Removed)> wrote in
message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      26th Aug 2006
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" <(E-Mail Removed)> wrote in
message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
> 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



 
Reply With Quote
 
clintonG
Guest
Posts: n/a
 
      26th Aug 2006
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in
> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
>> 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

>
>



 
Reply With Quote
 
=?Utf-8?B?R2FuZXNoIE11dGh1dmVsdQ==?=
Guest
Posts: n/a
 
      26th Aug 2006
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","Stylesheetname.css")
> End If
> End Sub
>
> --
> Regards
>
> John Timney (MVP)
>
>
>
> "Ganesh Muthuvelu" <(E-Mail Removed)> wrote in
> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
=?Utf-8?B?R2FuZXNoIE11dGh1dmVsdQ==?=
Guest
Posts: n/a
 
      26th Aug 2006
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" <(E-Mail Removed)> wrote in
> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      26th Aug 2006
Mine doesn't work?

"clintonG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in
>> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
>>> 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

>>
>>

>
>



 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      26th Aug 2006
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" <(E-Mail Removed)> wrote in
message news:E03B4CA2-08C9-43F8-A6D8-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in
>> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
>> > 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

>>
>>
>>



 
Reply With Quote
 
clintonG
Guest
Posts: n/a
 
      26th Aug 2006
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mine doesn't work?
>
> "clintonG" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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]" <(E-Mail Removed)> wrote in
>> message news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in
>>> message news:37C54983-F66A-4441-86B6-(E-Mail Removed)...
>>>> 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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding style runtime Bhuwan Bhaskar Microsoft ASP .NET 0 20th Jan 2009 03:25 AM
Adding a reference style =?Utf-8?B?bGVhbm5lYW5kc2Vhbg==?= Microsoft Word Document Management 0 19th Mar 2007 03:05 AM
adding c style dll to c# app saleh Microsoft C# .NET 3 19th Dec 2006 12:42 PM
adding c style dll to c# app saleh Microsoft C# .NET 0 19th Dec 2006 11:21 AM
Adding style to paragraph tag Anonymous Microsoft Frontpage 2 3rd Dec 2003 06:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:51 AM.