WebForms: dynamic (or absolute) script tags in MasterPages

Problem

When working with MasterPages, the general annoyance I encounter is that the script tags in the main processor relate to the consumption page.

So, for example, your JavaScript may work if your consumer page is at the root of your application, but when you put another page in a subfolder, the relative path breaks and JavaScript is not found. And there is no way to use the absolute paths that I know of in this case.

Last time I decided to attack this and find a good solution.

Suggested Solutions

I tried one strategy that revolved around calling ClientScriptManager.RegisterClientScriptInclude on Page_Load, but this did not seem to display anything (provided, my understanding of the plumbing associated with this is incomplete).

I tried another one that looked something like this:

<script language="javascript" src='<%= ResolveClientUrl("~/js/ddnmenu.js") %>' type="text/javascript"></script> 

... But this throws an exception: the Controls collection cannot be modified because the control contains blocks of code.

Working (but somewhat ugly) code

So, in the end, I came across literal control in my head, where I am doing the corresponding Html:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.SetupLiteralScriptsTag() End Sub Private Sub SetupLiteralScriptsTag() 'Build the script tags to import our JavaScript Dim Builder As New StringBuilder Builder.AppendLine(String.Format("<script type=""text/javascript"" src=""{0}""""></script>", ResolveClientUrl("~/js/jquery-1.3.2.min.js"))) Builder.AppendLine(String.Format("<script type=""text/javascript"" src=""{0}""""></script>", ResolveClientUrl("~/js/jquery.corners.min.js"))) Builder.AppendLine(String.Format("<script type=""text/javascript"" src=""{0}""""></script>", ResolveClientUrl("~/js/bg.pos.js"))) Builder.AppendLine(String.Format("<script type=""text/javascript"" src=""{0}""""></script>", ResolveClientUrl("~/js/moonstone.js"))) Me.LiteralScriptTags.Text = Builder.ToString End Sub 

This works, but I'm not burning about it, as it seems like too many workarounds for what should be an extremely common problem. Is there a better way?

+6
webforms master-pages
source share
5 answers

I use relative application syntax everywhere. It has the disadvantage that if you change the name / path of the application, you have a lot of work to update all your URLs.

<script language = "javascript" src = "/MyAppName/Includes/MyJavascriptFile.js">

or if you were working on a root application, then:

<script language = "javascript" src = "/Includes/MyJavascriptFile.js">

+3
source share

You can add a script writer to the main page and include javascript files through this:

 <asp:ScriptManager ...> <Scripts> <asp:ScriptReference Path="~/js/ddnmenu.js" /> </Scripts> </asp:ScriptManager> 

Another advantage of this approach is that you can add a ScriptManagerProxy control to your content pages (and user controls) to include any additional scripts.

Some third-party replacements for ASP: ScriptManager (for example, from telerik RadControls or from the Ajax Management Toolkit ) offer even more features, such as combining all included java script files into a single file (thus reducing the number of HTTP requests needed to load the page).

Edit : Using ScriptManager has some other advantages, for example. you can send debug versions or version versions of your scripts to a browser or culture-specific scripts, etc. etc. Check out this page on MSDN for an overview.

+7
source share

I am using Page.resolveUrl("~/somefile.js");

works like a champion

+1
source share

You can always refer to a script from the root. IE:

 <script language="javascript" src="/scripts/file.js"></script> 

Any page in your application will then receive javascript correctly.

In addition, since you are working with ASP.NET, you can use the “~” character to provide application-based relative paths. For more information, see the Rick Strahl article on ASP.NET paths.

0
source share

The collection of controls cannot be modified because the control contains code blocks

This error sounds familiar. This page helped me when I made my way through this

0
source share

All Articles