Asp.net 后台添加CSS、JS、Meta
把代码放在基类中,调用
#region##添加JS文件
/// <summary>
/// 添加JS文件
/// 创建人:Porschev
/// 创建时间:2021-1-25
/// </summary>
/// <param name="page">页面</param>
/// <param name="url">路径</param>
public void AddScript(System.Web.UI.Page page, string url)
{
HtmlGenericControl JsControl = new HtmlGenericControl("script");
JsControl.Attributes.Add("type", "text/javascript");
JsControl.Attributes.Add("src", url);
page.Header.Controls.Add(JsControl);
}
#endregion
#region##添加CSS文件
/// <summary>
/// 添加CSS文件
/// 创建人:Posrchev
/// 创建时间:2021-1-25
/// </summary>
/// <param name="page">页面</param>
/// <param name="url">路径</param>
public void AddCss(System.Web.UI.Page page, string url)
{
HtmlLink CssControl = new HtmlLink();
CssControl.Href = url;
CssControl.Attributes.Add("rel", "stylesheet");
CssControl.Attributes.Add("type", "text/css");
page.Header.Controls.Add(CssControl);
}
#endregion
#region##添加Meta标签
/// <summary>
/// 添加Meta标签
/// 创建人:Posrchev
/// 创建时间:2021-1-25
/// </summary>
/// <param name="page">页面</param>
/// <param name="name">名</param>
/// <param name="content">正文</param>
public void AddMeta(System.Web.UI.Page page,string name,string content)
{
System.Web.UI.HtmlControls.HtmlMeta meta = new System.Web.UI.HtmlControls.HtmlMeta();
meta.Name = name;
meta.Content = content;
page.Header.Controls.Add(meta);
}
#endregion
这三个方法都可以放在公共类中,如果直接放在页面类中, Page参数可以不要
// 引入js
HtmlGenericControl scriptControl = new HtmlGenericControl("script");
scriptControl.Attributes.Add("type", "text/javascript");
scriptControl.Attributes.Add("language", "JavaScript");
scriptControl.Attributes.Add("src", "/js/JQuery.js");
Page.Header.Controls.Add(scriptControl);
// 引入样式表
Literal cssControl = new Literal();
cssControl.Text = "<link href=\"/style/css.css\" rel=\"stylesheet\" type=\"text/css\"/>";
Page.Header.Controls.Add(cssControl);