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.
 
Joined
Apr 12, 2008
Messages
3
Reaction score
0
one possible solution

You can certainly use code blocks to make the SRC dynamic, but there is a drawback, you can no longer add elements to the header (in my experience) . I have user controls that will need to add their own css links, so this was a problem.

Reading through all the suggestions here (thanks btw) I found 2 possible solutions.

I was able to add a LiteralControl using the following

Dim sctl As String = String.Format("<script type='text/javascript' src={0} ></script>", Page.ResolveUrl("~/_js/jquery-1.3.2.js"))

header.Controls.Add(New LiteralControl(sctl))

I was also able to just add a LiteralControl in the markup and then change its TEXT property in PageLoad. I like this a little better because there is a place holder in the markup.

One other thing that I didnt see mentioned, LINK tags do recognize the ~ in the HREF attr.

link href="~/_StyleSheets/StyleSheet1.css" rel="stylesheet" type="text/css" />

greg
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top