代码之家  ›  专栏  ›  技术社区  ›  splattne

ASP。NET母版页:如何在aspx的head部分插入标记?

  •  7
  • splattne  · 技术社区  · 17 年前

    我知道我可以 access the head section of a page which uses a masterpage programmatically

    仅举一例 (我想插入脚本和样式等):

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

    声明式 在aspx文件本身中 ?

    有时插入一个 客户端脚本 样式声明 或指向外部资源的链接。

    4 回复  |  直到 17 年前
        1
  •  22
  •   LukeH    17 年前

    您可以通过在 head ,就像你在 body 的页面。例如,在您的主页上:

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

    然后在页面本身中,就像这样:

    <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>
    
        2
  •  6
  •   Canavar    17 年前

    对于 你可以使用这个:

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

    对于 元标签 :

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

    您可以通过以下方式添加体内元素:

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

    希望这能有所帮助!

        3
  •  2
  •   Dead account    17 年前

    您可以在内容页面声明中声明页面标题。

    <%@ Title="Page Title" Page Language="C#" AutoEventWireup="true" CodeFile="Subpage.aspx.cs" Inherits="Subpage" MasterPageFile="~/MasterPage.master" %>
    
        4
  •  0
  •   shahkalpesh    17 年前

    我还没试过这个。

    例如 <%=myTitle%>

    推荐文章