7

I know I can access the head section of a page which uses a masterpage programmatically this way (in code behind):

This is only an example (I'd like to insert scripts and styles etc.):

this.Header.Title = "I just set the page's title";

Is there a simple way to do this in a declarative way on in the aspx file itself?

Sometimes it would be handy to insert a client script or a style declaration or a link to an external resource.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
splattne
  • 102,760
  • 52
  • 202
  • 249

4 Answers4

22

You can do this by using content regions in the head, in exactly the same way as you would in the body of the page. eg, In your masterpage:

<head>
    <link type="text/css" rel="stylesheet" href="/styles/common1.css" />
    <script type="text/javascript" src="/scripts/common1.js"></script>
    <asp:contentplaceholder id="ExtraStylesAndScripts" runat="server" />
</head>

And then in the page itself just something like:

<asp:content contentplaceholderid="ExtraStylesAndScripts" runat="server">    
    <link type="text/css" rel="stylesheet" href="/styles/extra1.css" />
    <link type="text/css" rel="stylesheet" href="/styles/extra2.css" />
    <script type="text/javascript" src="/scripts/extra1.js"></script>
    <script type="text/javascript" src="/scripts/extra2.js"></script>
</asp:content>
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 2
    D'oh! I'm such an idiot. I don't know, but I had this idea, that ContentPlaceholders could not be used in the head section... thanks a lot! – splattne Feb 20 '09 at 09:52
  • 1
    In VS2008 under 3.5 Visual Studio even does this for you when you create a MasterPage – Iain M Norman Feb 20 '09 at 10:41
6

For stylesheet you can use this :

HtmlLink cssRef = new HtmlLink();
cssRef.Href = "addins/main.css";
cssRef.Attributes["rel"] = "stylesheet";
cssRef.Attributes["type"] = "text/css";
Page.Header.Controls.Add(cssRef);

For Meta Tags :

HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "author";
metaTag.Content = "ScarletGarden";
Page.Header.Controls.Add(metaTag);

But there is no way to add external script files to header element.

You can add inside body element by :

if (!ClientScript.IsClientScriptIncludeRegistered("myExternalScript"))
{
   ClientScript.RegisterClientScriptInclude("myExternalScript", "js/myJSFile.js");
}

Hope this helps !

Canavar
  • 47,715
  • 17
  • 91
  • 122
2

You can declare the page title in the content page declaration.

<%@ Title="Page Title" Page Language="C#" AutoEventWireup="true" CodeFile="Subpage.aspx.cs" Inherits="Subpage" MasterPageFile="~/MasterPage.master" %>
Dead account
  • 19,587
  • 13
  • 52
  • 82
  • Thank you for your answer. Maybe my example is misleading. I'd like to insert other things, like ad-hoc styles for that individual page and javascript. – splattne Feb 20 '09 at 09:14
0

I haven't tried this.
But you can put HEAD element inside html with the enclosed string in asp style markup.

e.g. <%=myTitle%>

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • I'm not interested in the title (it was only an example). I want to insert scripts, styles etc. using intellisense. I don't want to do it programmatically. – splattne Feb 20 '09 at 08:50