473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
-----------------------------------
Nov 18 '05 #1
6 5468
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.H tmlControls.Htm lGenericControl or .HtmlContainerC ontrol), 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:OY******** ******@tk2msftn gp13.phx.gbl...
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
-----------------------------------

Nov 18 '05 #2
Hi Ken,

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

Protected MyStyle As _
System.Web.UI.H tmlControls.Htm lGenericControl

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
MyStyle.InnerTe xt = _
"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:HyperLin k id="HyperLink1 " runat="server"
NavigateUrl="ht tp://authors.aspalli ance.com/kenc/faq5.aspx">Hype rLink</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.H tmlControls.Htm lGenericControl
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
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:OY******** ******@tk2msftn gp13.phx.gbl...
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
-----------------------------------


Nov 18 '05 #3
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 HtmlGenericCont rol, 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]" <BA************ @sympatico.ca> wrote in message
news:OT******** ******@TK2MSFTN GP10.phx.gbl...
Hi Ken,

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

Protected MyStyle As _
System.Web.UI.H tmlControls.Htm lGenericControl

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
MyStyle.InnerTe xt = _
"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:HyperLin k id="HyperLink1 " runat="server"
NavigateUrl="ht tp://authors.aspalli ance.com/kenc/faq5.aspx">Hype rLink</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.H tmlControls.Htm lGenericControl
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
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:OY******** ******@tk2msftn gp13.phx.gbl...
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
-----------------------------------

Nov 18 '05 #4
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:Oo******** ******@TK2MSFTN GP09.phx.gbl...
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 HtmlGenericCont rol, but I don't
see
how you render it to the HTML page that goes out.

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


Nov 18 '05 #5
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 HtmlGenericCont rol 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]" <BA************ @sympatico.ca> wrote in message
news:Ou******** *****@TK2MSFTNG P12.phx.gbl...
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:Oo******** ******@TK2MSFTN GP09.phx.gbl...
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 HtmlGenericCont rol, but I don't
see
how you render it to the HTML page that goes out.

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

Nov 18 '05 #6
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:#V******** ******@TK2MSFTN GP09.phx.gbl...
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 HtmlGenericCont rol 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]" <BA************ @sympatico.ca> wrote in message
news:Ou******** *****@TK2MSFTNG P12.phx.gbl...
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:Oo******** ******@TK2MSFTN GP09.phx.gbl...
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 HtmlGenericCont rol, but I don't see
how you render it to the HTML page that goes out.

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


Nov 18 '05 #7

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

Similar topics

21
10942
by: Wladimir Borsov | last post by:
Assume I want to publish a longer article on a web page. One of the paragraphs I want to display with an indent. Read: All text lines of this paragraph should be displayed with (let's say 8) blanks from the "normal" left side. How do I do that without coding "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" ? Is there something like <TAB size=8> ..... </TAB> ?
5
9245
by: toylet | last post by:
Attached is some css codes for a website. It has 3 parts: top-bar, side-bar (on the left) and main-body. The top-bar has a mouseover menu called top-menu implemented via ul:hover. When the mouse pointer hovers over the top-menu, the bottom margin of the top-bar expands downwards. How could I make the hover menu to stack on top of the main-body, not expanding the bottom margin of the top-bar? I believe it has something to do with...
15
32679
by: Matthias Hullin | last post by:
Hi, I'm programming some PHP discussion board that is supposed to appear inside the content area of a proprietary CMS. As I need some more styles than the standard stylesheet provides, I just added a <style> tag inside my HTML code. It works fine in all the browsers I tested, but it seems to be more of a hack than a valid solution.
10
6857
by: Brian W | last post by:
Hi All, I have a web user control that, among other things, provides Print this page, and Email this page functionality I have this script that is to execute on the click of the asp:hyperlinks I have a function in a <SCRIPT> block that I want in the <head></head> section of the page. Unfortunately, RegisterClientScriptBlock, RegisterStartupScript don't always work, and when they do the script is placed inside the <form> tag (this...
7
12975
by: ericgla | last post by:
I am creating a web app using asp.net 2.0 where all pages are based a single master page. On some of the aspx pages I need to add javascript to the head tag in order to use Google maps. I tried this to access the header: In the master page: <HEAD runat="server"> In the aspx page: Page_Load
8
3087
by: deko | last post by:
<head> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"> <title>Home Page</title> <!--> <style#navlist a { height: 1em; } </style> <!--> </head> Is the statement above JavaScript? If not, what kind of script is it?
5
1505
by: JOsh Josh | last post by:
How do i convert my HTML to a CSS format...i put this image sequence togther in HTML but i want to be able to post it right...but i dont know how to go about doing that. Some one please help <html> <head> <style type="text/css"> p.ridge {border-color: #FFD700; position: absolute; top:690px; left: 260px; width: 100; border-style: ridge;text-align: center;font-family: times; color: #00000} b.ridge {border-color: #FFD700; position:...
3
1455
by: phpmel | last post by:
Hi guys, I have yet another question. I am working with this html form that uses a template. <head> //is greyed out //some greyed out <style >stuff is next <!-- InstanceEndEditable -->//greyed out </head>//greyed out <body>//greyed out
36
5103
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT do so, since this is not part of the specification. How then to you apply styles to entire columns? Surely you don't have to write <td class="behold"on every row item.
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.