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

前端 未结 4 816
闹比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: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 !

提交回复
热议问题