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

K

Ken Varn

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
-----------------------------------
 
T

Teemu Keiski

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
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
-----------------------------------
 
K

Ken Cox [Microsoft MVP]

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
 
K

Ken Varn

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 said:
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
 
K

Ken Cox [Microsoft MVP]

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
 
K

Ken Varn

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
 
B

bruce barker

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)
 
Joined
May 10, 2010
Messages
1
Reaction score
0
code

Dim style As New HtmlGenericControl()
style.TagName = "style"
style.Attributes.Add("type", "text/css")
style.InnerHtml = "<YOURSTYLEHERE>"
Page.Header.Controls.Add(style)
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top