Asp.Net网页头部动态加载标题、描述、关键字、css和js文件的方法

首先head标签上添加runat="server"。

protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "标题";
        //Description
        HtmlMeta desc = new HtmlMeta();
        desc.Name = "Description";
        desc.Content = "描述";
        Page.Header.Controls.Add(desc);
        //Keyword
        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = "关键字";
        Page.Header.Controls.Add(keywords);

        //js
        HtmlGenericControl autoCompleteScript = new HtmlGenericControl("script");
        autoCompleteScript.Attributes.Add("type", "text/javascript");
        autoCompleteScript.Attributes.Add("src", "/js/autocomplete/jquery.autocomplete.js");
        Page.Header.Controls.Add(autoCompleteScript);

        //css
        HtmlLink autoCompleteCss = new HtmlLink();
        autoCompleteCss.Href = "/js/autocomplete/jquery.autocomplete.css";
        autoCompleteCss.Attributes.Add("rel", "stylesheet");
        autoCompleteCss.Attributes.Add("type", "text/css");
        Page.Header.Controls.Add(autoCompleteCss);
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。