Inject <STYLE> element into <HEAD>?

Discussion in 'ASP .Net Web Controls' started by kw, Aug 25, 2004.

  1. kw

    kw Guest

    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
     
    kw, Aug 25, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%...
    > 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
    >
    >
     
    Ken Cox [Microsoft MVP], Aug 26, 2004
    #2
    1. Advertising

  3. kw

    kw Guest

    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 [Microsoft MVP]" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
     
    kw, Aug 26, 2004
    #3
  4. Oh. You didn't say what kind of control.

    "kw" <> wrote in message
    news:...
    > 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 [Microsoft MVP]" <> wrote in message
    > news:%...
    >> 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" <> wrote in message
    >> news:%...
    >> > 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
    >> >
    >> >

    >>

    >
    >
     
    Ken Cox [Microsoft MVP], Aug 26, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    823
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    784
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    434
  4. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    264
    Sam Fields
    Jan 16, 2004
  5. Peña, Botp

    inject does not inject last value

    Peña, Botp, Aug 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    198
    Peña, Botp
    Aug 7, 2006
Loading...

Share This Page