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

Discussion in 'ASP .Net' started by Ken Varn, Apr 23, 2004.

  1. Ken Varn

    Ken Varn Guest

    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
    -----------------------------------
     
    Ken Varn, Apr 23, 2004
    #1
    1. Advertising

  2. Ken Varn

    Teemu Keiski Guest

    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:...
    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
    -----------------------------------
     
    Teemu Keiski, Apr 23, 2004
    #2
    1. Advertising

  3. 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:...
    >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
    > -----------------------------------
    >
    >
     
    Ken Cox [Microsoft MVP], Apr 23, 2004
    #3
  4. Ken Varn

    Ken Varn Guest

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

    >
     
    Ken Varn, Apr 23, 2004
    #4
  5. 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:...
    > 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.
    >
     
    Ken Cox [Microsoft MVP], Apr 24, 2004
    #5
  6. Ken Varn

    Ken Varn Guest

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

    >
     
    Ken Varn, Apr 26, 2004
    #6
  7. Ken Varn

    bruce barker Guest

    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:#...
    > 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]" <> wrote in message
    > news:...
    > > 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:...
    > > > 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.
    > > >

    > >

    >
    >
     
    bruce barker, Apr 26, 2004
    #7
  8. Ken Varn

    robertbrooker

    Joined:
    May 10, 2010
    Messages:
    1
    code

    Dim style As New HtmlGenericControl()
    style.TagName = "style"
    style.Attributes.Add("type", "text/css")
    style.InnerHtml = "<YOURSTYLEHERE>"
    Page.Header.Controls.Add(style)
     
    robertbrooker, May 10, 2010
    #8
    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:
    784
    Brian W
    Jul 2, 2003
  2. Sergey Morkovkin
    Replies:
    5
    Views:
    17,337
    gamcfarlane
    Jul 14, 2009
  3. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    737
    Sherif ElMetainy
    Jul 10, 2004
  4. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    240
    Sam Fields
    Jan 16, 2004
  5. Sergey Morkovkin

    Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY

    Sergey Morkovkin, Sep 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    196
    Slava Tihonyuk
    Sep 15, 2003
Loading...

Share This Page