Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How do I dynamically add a <style> to the <head> section of ASP.NET page?

Reply
Thread Tools

How do I dynamically add a <style> to the <head> section of ASP.NET page?

 
 
Ken Varn
Guest
Posts: n/a
 
      04-23-2004
I want to add my own custom <STYLE> section in the <HEAD> section of my
ASP.NET page within a custom control. Can someone tell me how I can have my
custom control add tags to the <HEAD> section of the page dynamically when
the page is rendered?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      04-23-2004
Hi,

you'd need to make the <HEAD> element work at server-side, that is, specify
it with runat="server" attribute and an ID. With the ID (as typed
code-behind member, for example
System.Web.UI.HtmlControls.HtmlGenericControl or .HtmlContainerControl), you
can access the server-side HEAD element in code from the control.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke


"Ken Varn" <nospam> wrote in message
news:(E-Mail Removed)...
I want to add my own custom <STYLE> section in the <HEAD> section of my
ASP.NET page within a custom control. Can someone tell me how I can have my
custom control add tags to the <HEAD> section of the page dynamically when
the page is rendered?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------



 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      04-23-2004
Hi Ken,

If you want to add styles to an existing STYLE tag, in your codebehind, put
this:

Protected MyStyle As _
System.Web.UI.HtmlControls.HtmlGenericControl

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
MyStyle.InnerText = _
"A {text-decoration:none;color: #ff0000;}"
End Sub

In your .aspx HTML code, put this:

<HEAD>
<title>addstyle</title>
<style id="mystyle" runat="server">
</style>
</HEAD>
....

<form id="Form1" method="post" runat="server">
<asp:HyperLink id="HyperLink1" runat="server"
NavigateUrl="http://authors.aspalliance.com/kenc/faq5.aspx">HyperLink</asp:HyperLink>
</form>


If you don't have a STYLE tag at all in the HEAD and want to insert one, put
this:

Protected Head1 As _
System.Web.UI.HtmlControls.HtmlGenericControl
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Head1.InnerHtml = _
"<style> A {text-decoration:none;" & _
"color:#ff0000;}</style>"
End Sub

And this goes in the HTML

<HEAD id="head1" runat=server>


Watch out, because the VS.NET editor my try to alter the HTML code that you
inserted.

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto


"Ken Varn" <nospam> wrote in message
news:(E-Mail Removed)...
>I want to add my own custom <STYLE> section in the <HEAD> section of my
> ASP.NET page within a custom control. Can someone tell me how I can have
> my
> custom control add tags to the <HEAD> section of the page dynamically when
> the page is rendered?
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
>
>


 
Reply With Quote
 
Ken Varn
Guest
Posts: n/a
 
      04-23-2004
Your second example is more of what I am looking for, but I am a little
confused by your example.

There is no <STYLE> in my <HEAD> section at all. I need to add the <STYLE>
section dynamically with my custom control because of some interaction that
I have with custom JavaScript on the client side.

I want to do this without touching the aspx file. I would prefer that my
custom control add the <style> section to the <head> of the page that uses
it.

Looking at your example, you create an HtmlGenericControl, but I don't see
how you render it to the HTML page that goes out.



--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Ken Cox [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Ken,
>
> If you want to add styles to an existing STYLE tag, in your codebehind,

put
> this:
>
> Protected MyStyle As _
> System.Web.UI.HtmlControls.HtmlGenericControl
>
> Private Sub Page_Load _
> (ByVal sender As System.Object, _
> ByVal e As System.EventArgs) _
> Handles MyBase.Load
> MyStyle.InnerText = _
> "A {text-decoration:none;color: #ff0000;}"
> End Sub
>
> In your .aspx HTML code, put this:
>
> <HEAD>
> <title>addstyle</title>
> <style id="mystyle" runat="server">
> </style>
> </HEAD>
> ...
>
> <form id="Form1" method="post" runat="server">
> <asp:HyperLink id="HyperLink1" runat="server"
>

NavigateUrl="http://authors.aspalliance.com/kenc/faq5.aspx">HyperLink</asp:H
yperLink>
> </form>
>
>
> If you don't have a STYLE tag at all in the HEAD and want to insert one,

put
> this:
>
> Protected Head1 As _
> System.Web.UI.HtmlControls.HtmlGenericControl
> Private Sub Page_Load _
> (ByVal sender As System.Object, _
> ByVal e As System.EventArgs) _
> Handles MyBase.Load
> Head1.InnerHtml = _
> "<style> A {text-decoration:none;" & _
> "color:#ff0000;}</style>"
> End Sub
>
> And this goes in the HTML
>
> <HEAD id="head1" runat=server>
>
>
> Watch out, because the VS.NET editor my try to alter the HTML code that

you
> inserted.
>
> Does this help?
>
> Ken
> Microsoft MVP [ASP.NET]
> Toronto
>
>
> "Ken Varn" <nospam> wrote in message
> news:(E-Mail Removed)...
> >I want to add my own custom <STYLE> section in the <HEAD> section of my
> > ASP.NET page within a custom control. Can someone tell me how I can

have
> > my
> > custom control add tags to the <HEAD> section of the page dynamically

when
> > the page is rendered?
> >
> > --
> > -----------------------------------
> > Ken Varn
> > Senior Software Engineer
> > Diebold Inc.
> >
> > EmailID = varnk
> > Domain = Diebold.com
> > -----------------------------------
> >
> >

>



 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      04-23-2004
Hi Ken,

It looks like the second example is what you want.

It is starts with only the HEAD tag and adds a STYLE (with the style code)
to it dynamically.

Ken

"Ken Varn" <nospam> wrote in message
news:(E-Mail Removed)...
> Your second example is more of what I am looking for, but I am a little
> confused by your example.
>
> There is no <STYLE> in my <HEAD> section at all. I need to add the
> <STYLE>
> section dynamically with my custom control because of some interaction
> that
> I have with custom JavaScript on the client side.
>
> I want to do this without touching the aspx file. I would prefer that my
> custom control add the <style> section to the <head> of the page that uses
> it.
>
> Looking at your example, you create an HtmlGenericControl, but I don't
> see
> how you render it to the HTML page that goes out.
>
>
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>


 
Reply With Quote
 
Ken Varn
Guest
Posts: n/a
 
      04-26-2004
Sorry, but I still do not quite understand what you are trying to convey. I
am a bit of a newbie at this stuff.

My custom control needs to add a <style> block to the control's owner page.
I would prefer this to happen without the owner page having to do anything
special except add my control. The only place that a <style> block can be
defined is within the <body> tags. How can I access my control's owner page
<body> section and add my <style> block to it?

I am assuming that I need to access the Page object of my control's owner
and add my HtmlGenericControl to it, but I get an exception stating that a
control cannot modify its parent's controls collection.


--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Ken Cox [Microsoft MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Ken,
>
> It looks like the second example is what you want.
>
> It is starts with only the HEAD tag and adds a STYLE (with the style code)
> to it dynamically.
>
> Ken
>
> "Ken Varn" <nospam> wrote in message
> news:(E-Mail Removed)...
> > Your second example is more of what I am looking for, but I am a little
> > confused by your example.
> >
> > There is no <STYLE> in my <HEAD> section at all. I need to add the
> > <STYLE>
> > section dynamically with my custom control because of some interaction
> > that
> > I have with custom JavaScript on the client side.
> >
> > I want to do this without touching the aspx file. I would prefer that

my
> > custom control add the <style> section to the <head> of the page that

uses
> > it.
> >
> > Looking at your example, you create an HtmlGenericControl, but I don't
> > see
> > how you render it to the HTML page that goes out.
> >
> >
> >
> > --
> > -----------------------------------
> > Ken Varn
> > Senior Software Engineer
> > Diebold Inc.
> >

>



 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      04-26-2004
there is no real supported method. by default the owner page's style (if it
hand any) is include in the literal that defines the html until the form
statement. while asp.net has added support for shared javascript, it has
ignored styles.

your custom control, could walk the chain of parents to get to the page
contols collect, scan literals looking fr style, then updated the html if
found.

but a better solution, is to create a template page, that all your pages
inherit from, and add <style id=mystyle runat=server> tag to the template.
then create a static method that add style commands to this tag.

-- bruce (sqlwork.com)




"Ken Varn" <nospam> wrote in message
news:#(E-Mail Removed)...
> Sorry, but I still do not quite understand what you are trying to convey.

I
> am a bit of a newbie at this stuff.
>
> My custom control needs to add a <style> block to the control's owner

page.
> I would prefer this to happen without the owner page having to do anything
> special except add my control. The only place that a <style> block can be
> defined is within the <body> tags. How can I access my control's owner

page
> <body> section and add my <style> block to it?
>
> I am assuming that I need to access the Page object of my control's owner
> and add my HtmlGenericControl to it, but I get an exception stating that a
> control cannot modify its parent's controls collection.
>
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
> "Ken Cox [Microsoft MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi Ken,
> >
> > It looks like the second example is what you want.
> >
> > It is starts with only the HEAD tag and adds a STYLE (with the style

code)
> > to it dynamically.
> >
> > Ken
> >
> > "Ken Varn" <nospam> wrote in message
> > news:(E-Mail Removed)...
> > > Your second example is more of what I am looking for, but I am a

little
> > > confused by your example.
> > >
> > > There is no <STYLE> in my <HEAD> section at all. I need to add the
> > > <STYLE>
> > > section dynamically with my custom control because of some interaction
> > > that
> > > I have with custom JavaScript on the client side.
> > >
> > > I want to do this without touching the aspx file. I would prefer that

> my
> > > custom control add the <style> section to the <head> of the page that

> uses
> > > it.
> > >
> > > Looking at your example, you create an HtmlGenericControl, but I

don't
> > > see
> > > how you render it to the HTML page that goes out.
> > >
> > >
> > >
> > > --
> > > -----------------------------------
> > > Ken Varn
> > > Senior Software Engineer
> > > Diebold Inc.
> > >

> >

>
>



 
Reply With Quote
 
robertbrooker robertbrooker is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-10-2010
Dim style As New HtmlGenericControl()
style.TagName = "style"
style.Attributes.Add("type", "text/css")
style.InnerHtml = "<YOURSTYLEHERE>"
Page.Header.Controls.Add(style)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
section with in a section config file and reading that config file kampy Python 9 10-19-2012 10:59 PM
Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY Sergey Morkovkin ASP .Net 5 07-14-2009 04:50 PM
title section dynamically Dariusz Tomon ASP .Net 8 05-15-2006 08:33 PM
Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY Sergey Morkovkin ASP .Net Web Controls 4 09-15-2003 07:47 AM



Advertisments