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?