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

自古美人都是妖i 提交于 2019-12-30 04:38:19

问题


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?


回答1:


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>



回答2:


Have you tried:

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



回答3:


As per ScottGu,

One tip to take advantage of is the relative path fix-up support provided by the head runat="server" control. You can use this within Master Pages to easily reference a .CSS stylesheet that is re-used across the entire project (regardless of whether the project is root referenced or a sub-application):

The path fix-up feature of the head control will then take the relative .CSS stylesheet path and correctly output the absolute path to the stylesheet at runtime regardless of whether it is a root referenced web-site or part of a sub-application.



来源:https://stackoverflow.com/questions/183859/preferred-way-to-include-relative-reference-to-javascript-in-vs-2008-nested-mast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!