Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY

S

Sergey Morkovkin

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
 
K

Kevin Spencer

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 said:
Hi, guys!
Does anyone knows the resolution of a problem when i need to insert <link>
or said:
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.
 
A

Anatoly

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 said:
Hi, guys!
Does anyone knows the resolution of a problem when i need to insert <link>
or said:
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.
 
E

Ezra Epstein

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 said:
Hi, guys!
Does anyone knows the resolution of a problem when i need to insert <link>
or said:
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.
 
S

Slava Tihonyuk

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 said:
Hi, guys!
Does anyone knows the resolution of a problem when i need to insert <link>
or said:
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.
 

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

Top