tags:

views:

1073

answers:

5

I have a page which has a masterpage. Do I put script and link tags inside the asp:content place holders or outside or does it matter.

When I put it outside, I get the following warning:

Only Content controls are allowed directly in a content page that contains Content controls.

+1  A: 

Outside. Tthe inside of the ContentPlaceholders is going to be replaced with content from your pages anyhow so it doesn't make much sense to put anything in there.

Andrew Hare
+1  A: 

outside. in the master page

The place holders are the wrapping controls for pages which descend from master pages.

Richard
+1  A: 

If you are referring to <asp:Content /> tags, you can't put anything outside of them in an .aspx page. So you're limited to putting them inside the <asp:Content /> tag. If you want <script /> and <link /> tags you need to either put a <asp:ContentPlaceHolder /> in the <head> of your master page, or add them dynamically via the Page's Controls collection.

Sean Bright
+1  A: 

If it's scripts and links for all pages, it should go outside any ContentPlaceHolders. If it's scripts and links for this page, it should go inside a Content inside the head. If it's default scripts, put it inside a head ContentPlaceHolder, and it can be replaced by the child page if needed. (VS usually complains about a ContentPlaceHolder in the head, but it works fine for me).

// master Page
<head runat="server">
   <asp:ContentPlaceHolder id="head" runat="server">
      <!-- Default scripts and CSS -->
      <link rel="stylesheet" type="text/css" href="default.css" />
      <script type="text/javascript" src="jquery.js"></script>
   </asp:ContentPlaceHolder>
   <!-- Mandatory scripts and css -->
   <link rel="stylesheet" type="text/css" href="all.css" />
   <script type="text/javascript" src="all.js"></script>
</head>
<body>
   Master Page!
   <asp:ContentPlaceHolder id="body" runat="server" />
</body>

// Child (no JQuery)
<asp:Content ContentPlaceHolderID="head" runat="server">
   <link rel="stylesheet" type="text/css" href="default.css" />
   <!-- Don't need JQuery -->
   <script type="text/javascript" src="prototype.js"></script>
</asp:Content>
<asp:Content ContentPlaceHolderID="body" runat="server">
   Child Page!
</asp:Content>

// Child 2 (with JQuery)
<asp:Content ContentPlaceHolderID="body" runat="server">
   Child Page!
</asp:Content>
Mark Brackett
It seems to work even though it's complaining and I have it outside, but what if I don't have a head placeholder.
Xaisoft
Add one. ;) I usually end up with at least 3 placeholders: 1 head for default scripts and styles, 1 head for adding scripts and styles, and 1 content.
Mark Brackett
+3  A: 

I can see five (or 8) ways of doing it:

  1. In the codebehind (.cs or .vb) using:
    • Scriptmanager.RegisterClientScriptinclude - using an absolute/relative path
    • Scriptmanager.RegisterClientScriptInclude - using an embedded resource
    • Scriptmanager.RegisterSlientScriptBlock - with your source inside
  2. Adding it inline to your ASPX page in the designer
  3. Sticking it inside the asp:content where the content lives inside the body tag.
  4. Sticking it inside the asp:content where the content lives inside the head (You've said this isn't an option, so ignore this).
  5. Adding it programmatically using the ScriptManager inside a control you use on the page as above.

"Only Content controls are allowed directly in a content page that contains Content controls" - did you forget the runat="server"?

Chris S