Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do script tags and link tags go inside asp:content or outside

Tags:

asp.net

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.

like image 487
Xaisoft Avatar asked Mar 24 '09 21:03

Xaisoft


People also ask

Where should I put my script tags?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.

Should scripts be inside or outside body?

You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Do scripts go inside the body tag?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body.

What goes inside script tag?

The script tag when used to import an external . js file contains just the src and type attributes, and an endtag. Nothing goes inside the element.


4 Answers

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"?

like image 108
Chris S Avatar answered Sep 21 '22 05:09

Chris S


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>
like image 34
Mark Brackett Avatar answered Sep 19 '22 05:09

Mark Brackett


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.

like image 36
Sean Bright Avatar answered Sep 19 '22 05:09

Sean Bright


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.

like image 33
Andrew Hare Avatar answered Sep 20 '22 05:09

Andrew Hare