Javascript – Preferred way to include relative reference to JavaScript in VS 2008 nested Masterpage

asp.netjavascriptmaster-pagesnestedrelative-path

Our base Masterpage has something like the following

  <head runat="server">
   <title></title>

   <script type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/actions.js")%>"></script>
   <script type="text/javascript" src="<%= Page.ResolveClientURL("~/javascript/jquery/jquery-1.2.6.min.js")%>"></script>
   <asp:contentplaceholder id="cph_htmlhead" runat="server">

   </asp:contentplaceholder>
  </head>

If this Masterpage is the Masterpage for an ASPX page things work fine.

If this Masterpage is the Masterpage for a child Masterpage and then a new ASPX page uses the child Masterpage as it's MasterPage we see:

Server Error in '' Application.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>).

What is the preferred way to include global resources (Javascript/CSS) in a base Masterpage preserving tilde(~) style relative pathing?

Best Answer

Use the ScriptManager server control:

  <asp:ScriptManager ID="myScriptManager" runat="server">
    <Scripts>
      <asp:ScriptReference Path = "~/javascript/actions.js" /> 
      <asp:ScriptReference Path = "~/javascript/jquery/jquery-1.2.6.min.js" />
    </Scripts>
  </asp:ScriptManager>