Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY

Discussion in 'ASP .Net Web Controls' started by Sergey Morkovkin, Sep 11, 2003.

  1. Hi, guys!
    Does anyone knows the resolution of a problem when i need to insert <link> or <script> tag into HEAD section dynamically?
    I was trying few ways to do this: 1) RegisterClientScriptBlock inserts script right after form.
    2) RegisterStartupScript inserts script before post form.
    3) Page.Controls.Add(new LiteralControl("<LINK rel='stylesheet' href='include/inputbox.css' type='text/css'>")); (or AddAt method) Works, but inserts only the last one tag to the end of a page.

    P.S. I need to INSERT new LINK or SCRIPT tag but to change the href or src attribute of an existing tag.

    --
    Sergey Morkovkin, Web Project Leader
    Celline Ltd. - World of content in real time
    http://www.celline.com.ua
    +380 44 234 65 36
    +380 67 280 11 22
    Sergey Morkovkin, Sep 11, 2003
    #1
    1. Advertising

  2. Add a "runat=server" attribute to your <head> tag, give it an id, and wire
    it up to an HtmlGenericControl object in your CodeBehind. Then you can
    manipulate it like any other Control, as well as adding to its' Controls
    Collection.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "Sergey Morkovkin" <> wrote in message
    news:eek:...
    > Hi, guys!
    > Does anyone knows the resolution of a problem when i need to insert <link>

    or <script> tag into HEAD section dynamically?
    > I was trying few ways to do this: 1) RegisterClientScriptBlock inserts

    script right after form.
    > 2) RegisterStartupScript inserts script before post form.
    > 3) Page.Controls.Add(new LiteralControl("<LINK rel='stylesheet'

    href='include/inputbox.css' type='text/css'>")); (or AddAt method) Works,
    but inserts only the last one tag to the end of a page.
    >
    > P.S. I need to INSERT new LINK or SCRIPT tag but to change the href or src

    attribute of an existing tag.
    >
    > --
    > Sergey Morkovkin, Web Project Leader
    > Celline Ltd. - World of content in real time
    > http://www.celline.com.ua
    > +380 44 234 65 36
    > +380 67 280 11 22
    Kevin Spencer, Sep 11, 2003
    #2
    1. Advertising

  3. Sergey Morkovkin

    Anatoly Guest

    I tried this:
    Add manually into aspx file id attribute to HEAD tag "myHead", add also
    runat=server attribute.
    in code behind add line inside WebForm:

    protected System.Web.UI.HtmlControls.HtmlGenericControl myHead;

    When You can write on Page_Load:
    myHead.innerHtml += [anything you want to add in HEAD]

    HTH


    "Sergey Morkovkin" <> wrote in message
    news:eek:...
    > Hi, guys!
    > Does anyone knows the resolution of a problem when i need to insert <link>

    or <script> tag into HEAD section dynamically?
    > I was trying few ways to do this: 1) RegisterClientScriptBlock inserts

    script right after form.
    > 2) RegisterStartupScript inserts script before post form.
    > 3) Page.Controls.Add(new LiteralControl("<LINK rel='stylesheet'

    href='include/inputbox.css' type='text/css'>")); (or AddAt method) Works,
    but inserts only the last one tag to the end of a page.
    >
    > P.S. I need to INSERT new LINK or SCRIPT tag but to change the href or src

    attribute of an existing tag.
    >
    > --
    > Sergey Morkovkin, Web Project Leader
    > Celline Ltd. - World of content in real time
    > http://www.celline.com.ua
    > +380 44 234 65 36
    > +380 67 280 11 22
    Anatoly, Sep 11, 2003
    #3
  4. Sergey Morkovkin

    Ezra Epstein Guest

    Another easy way if you're just looking to change the href of a LINK tag is:

    <LINK rel="stylesheet" type="text/css" href="<%= MyDynamicLink %> ">

    It's a bit of a hybrid approach: using ASP's <%= %> in an ASP.NET page.

    In the code-behind you'd have

    C#: <snip>
    public string MyDynamicLink
    {
    get { return "style.css" ); } // your variable goes here instead of
    the literal "style.css"
    }
    </snip>

    Ezra E.

    "Sergey Morkovkin" <> wrote in message
    news:eek:...
    > Hi, guys!
    > Does anyone knows the resolution of a problem when i need to insert <link>

    or <script> tag into HEAD section dynamically?
    > I was trying few ways to do this: 1) RegisterClientScriptBlock inserts

    script right after form.
    > 2) RegisterStartupScript inserts script before post form.
    > 3) Page.Controls.Add(new LiteralControl("<LINK rel='stylesheet'

    href='include/inputbox.css' type='text/css'>")); (or AddAt method) Works,
    but inserts only the last one tag to the end of a page.
    >
    > P.S. I need to INSERT new LINK or SCRIPT tag but to change the href or src

    attribute of an existing tag.
    >
    > --
    > Sergey Morkovkin, Web Project Leader
    > Celline Ltd. - World of content in real time
    > http://www.celline.com.ua
    > +380 44 234 65 36
    > +380 67 280 11 22
    Ezra Epstein, Sep 13, 2003
    #4
  5. Hi Sergey

    Maybe you can add this
    HtmlGenericControl head = (HtmlGenericControl)FindControl("TheHead");

    head.Controls.Add(new LiteralControl("<LINK rel='stylesheet'
    href='include/inputbox.css' type='text/css'>"));

    :)

    I tried to program title tag this way too:)

    Slava



    "Sergey Morkovkin" <> wrote in message
    news:eek:...
    > Hi, guys!
    > Does anyone knows the resolution of a problem when i need to insert <link>

    or <script> tag into HEAD section dynamically?
    > I was trying few ways to do this: 1) RegisterClientScriptBlock inserts

    script right after form.
    > 2) RegisterStartupScript inserts script before post form.
    > 3) Page.Controls.Add(new LiteralControl("<LINK rel='stylesheet'

    href='include/inputbox.css' type='text/css'>")); (or AddAt method) Works,
    but inserts only the last one tag to the end of a page.
    >
    > P.S. I need to INSERT new LINK or SCRIPT tag but to change the href or src

    attribute of an existing tag.
    >
    > --
    > Sergey Morkovkin, Web Project Leader
    > Celline Ltd. - World of content in real time
    > http://www.celline.com.ua
    > +380 44 234 65 36
    > +380 67 280 11 22
    Slava Tihonyuk, Sep 15, 2003
    #5
    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:
    773
    Brian W
    Jul 2, 2003
  2. Sergey Morkovkin
    Replies:
    5
    Views:
    17,320
    gamcfarlane
    Jul 14, 2009
  3. Rick
    Replies:
    3
    Views:
    536
    bruce barker \(sqlwork.com\)
    May 10, 2006
  4. Luurs
    Replies:
    1
    Views:
    2,412
    Alan Silver
    Aug 8, 2006
  5. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    235
    Sam Fields
    Jan 16, 2004
Loading...

Share This Page