ASP.NET masterpages: how to insert markup in the head section inside the aspx?

前端 未结 4 747
闹比i
闹比i 2020-12-19 05:56

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 ins

相关标签:
4条回答
  • 2020-12-19 06:28

    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>
    
    0 讨论(0)
  • 2020-12-19 06:29

    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 !

    0 讨论(0)
  • 2020-12-19 06:33

    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" %>
    
    0 讨论(0)
  • 2020-12-19 06:49

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

    e.g. <%=myTitle%>

    0 讨论(0)
提交回复
热议问题