views:

719

answers:

2

I have an old ASP.NET 1.1 site that I am maintaining. We are working with Google to place analytics code on all pages. Since I can't take advantage of master pages in 1.1, I have my pages include headers/footers/sidebars with User Controls.

What came to mind first is to place the JavaScript in my footer ascx control so it appears on every page. But I don't think I can link to a JavaScript file from a user control.

Any ideas on what I can do to get this js code placed on every page in my site?

+4  A: 

What keeps you from simply referencing your script in the user control?

<asp:SomeControl ID="SomeControl1" runat="server>
  <script src="some.js" type="text/javascript"></script>
</asp:SomeControl>

You could also do this:

protected void Page_Load(object sender, EventArgs e)
{
  Literal some_js = new Literal();
  some_js = "<script type='text/javascript' src='some.js'></script>";
  this.Header.Controls.Add(some_js);
}

(Obviously, the second approach would still force you to modify the pages themselves, unless they inherit from a common parent you control.)

Tomalak
Don't think the second approach would work on ASP.NET 1.1
Cristian Libardo
A: 

Create a base page class and load the script in base page. Further inherit all pages from base page.

Other way could be same as that suggested by Tomalak

HtmlGenericControl jscriptFile = new HtmlGenericControl();
jscriptFile.TagName = "script";
jscriptFile.Attributes.Add("type", "text/javascript");
jscriptFile.Attributes.Add("language", "javascript"); 
jscriptFile.Attributes.Add("src", ResolveUrl("myscriptFile.js"));
this.Page.Header.Controls.Add(myJs);
Pradeep Kumar Mishra
The solution for me was to create a base page and register the javascript.
Scott