Inject <STYLE> element into <HEAD>?

K

kw

anyone know of some way for a control to add a STYLE element into the HEAD
section?

I would think that MS would have thought of the equivalent of a
Page.RegisterHead method like RegisterStartupScript or
RegisterStartupScript
 
K

Ken Cox [Microsoft MVP]

Inserting a Style isn't difficult. Just use an HtmlGenericControl and set
its InnerText value to the style. See the code below.

Ken
Microsoft MVP [ASP.NET]



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

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Style1.InnerText = _
"body{color:lime;font-family:'Comic Sans MS';}"
End Sub

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>style</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<style runat="server" id="Style1">
</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
This is the styled text
</form>
</body>
</HTML>
 
K

kw

Thanks Ken, but that doesn't work for a Server Control:

protected override void Render(HtmlTextWriter writer){
System.Web.UI.HtmlControls.HtmlGenericControl c =new
System.Web.UI.HtmlControls.HtmlGenericControl("STYLE");
c.Attributes.Add("TYPE","text/css");
c.InnerText="div.class element {background-color:White;}";
c.RenderControl(writer);

....

This just sticks the STYLE element into the body. (Note that a STYLE goes
into the HEAD according to W3C).




Ken Cox said:
Inserting a Style isn't difficult. Just use an HtmlGenericControl and set
its InnerText value to the style. See the code below.

Ken
Microsoft MVP [ASP.NET]



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

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Style1.InnerText = _
"body{color:lime;font-family:'Comic Sans MS';}"
End Sub

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>style</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<style runat="server" id="Style1">
</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
This is the styled text
</form>
</body>
</HTML>


kw said:
anyone know of some way for a control to add a STYLE element into the HEAD
section?

I would think that MS would have thought of the equivalent of a
Page.RegisterHead method like RegisterStartupScript or
RegisterStartupScript
 
K

Ken Cox [Microsoft MVP]

Oh. You didn't say what kind of control.

kw said:
Thanks Ken, but that doesn't work for a Server Control:

protected override void Render(HtmlTextWriter writer){
System.Web.UI.HtmlControls.HtmlGenericControl c =new
System.Web.UI.HtmlControls.HtmlGenericControl("STYLE");
c.Attributes.Add("TYPE","text/css");
c.InnerText="div.class element {background-color:White;}";
c.RenderControl(writer);

...

This just sticks the STYLE element into the body. (Note that a STYLE goes
into the HEAD according to W3C).




Ken Cox said:
Inserting a Style isn't difficult. Just use an HtmlGenericControl and set
its InnerText value to the style. See the code below.

Ken
Microsoft MVP [ASP.NET]



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

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Style1.InnerText = _
"body{color:lime;font-family:'Comic Sans MS';}"
End Sub

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>style</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<style runat="server" id="Style1">
</style>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
This is the styled text
</form>
</body>
</HTML>


kw said:
anyone know of some way for a control to add a STYLE element into the HEAD
section?

I would think that MS would have thought of the equivalent of a
Page.RegisterHead method like RegisterStartupScript or
RegisterStartupScript
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top