最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Preferred way to include relative reference to JavaScript in VS 2008 nested Masterpage - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Oct 11, 2008 at 4:57 Gulzar Nazim 52.2k26 gold badges130 silver badges170 bronze badges asked Oct 8, 2008 at 17:18 KP.KP. 2,16823 silver badges21 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

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>

Have you tried:

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

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.

发布评论

评论列表(0)

  1. 暂无评论