Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 578
KP. Avatar asked Oct 08 '08 17:10

KP.


People also ask

How do I reference properties and methods on the master page?

The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public properties and public methods. You can reference controls on the master page independently of referencing public members. Add a @ MasterType directive in the content page.

Can I write code in content pages that references the Master?

You can write code in content pages that references properties, methods, and controls in the master page, with some restrictions. The rule for properties and methods is that you can reference them if they are declared as public members of the master page.

How do I get a reference to two controls on the Master?

Use the FindControl method, using the value returned by the Master property as the naming container. The following code example shows how to use the FindControl method to get a reference to two controls on the master page, a TextBox control and a Label control.

How do I strongly Type A content page's Master property?

This directive causes the content page's Master property to be strongly typed. Write code that uses the master page's public member as a member of the Master property, as in this example, which assigns the value of a public property named CompanyName from the master page to a text box on the content page:


2 Answers

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>
like image 148
Kon Avatar answered Nov 07 '22 06:11

Kon


Have you tried:

<script type="text/javascript" src='<%= Page.ResolveClientUrl("~/javascript/actions.js") %>'></script>
like image 32
Shawn Miller Avatar answered Nov 07 '22 06:11

Shawn Miller