首页 > 代码库 > asp.net 后台添加css,js,meta

asp.net 后台添加css,js,meta

首先导入命名空间 using System.Web.UI.HtmlControls;

 1         /// <summary>
 2         /// 添加JS脚本链接
 3         /// </summary>
 4         /// <param name="page">页面</param>
 5         /// <param name="url">路径</param> 
 6         public void AddJS(System.Web.UI.Page page, string url)
 7         {
 8                 HtmlGenericControl jsControl = new HtmlGenericControl("script");
 9                 jsControl.Attributes.Add("type", "text/javascript");
10                 jsControl.Attributes.Add("src", url);
11                 page.Header.Controls.Add(jsControl);
12         }
13 
14         /// <summary>
15         /// 添加JS脚本内容
16         /// </summary>
17         /// <param name="page">页面</param>
18         /// <param name="content">脚本内容</param> 
19         public void AddScript(System.Web.UI.Page page, string content)
20         {
21                 HtmlGenericControl scriptControl = new HtmlGenericControl("script");
22                 scriptControl.Attributes.Add("type", "text/javascript");
23                 scriptControl.InnerHtml = content;
24                 page.Header.Controls.Add(scriptControl);
25         }
26 
27         /// <summary>
28         /// 添加CSS样式链接
29         /// </summary>
30         /// <param name="page">页面</param>
31         /// <param name="url">路径</param> 
32         public void AddCss(System.Web.UI.Page page, string url)
33         {
34                 HtmlLink link = new HtmlLink();
35                 link.Href =http://www.mamicode.com/ url;
36                 link.Attributes.Add("rel", "stylesheet");
37                 link.Attributes.Add("type", "text/css");
38                 page.Header.Controls.Add(link);
39         }
40 
41         /// <summary>
42         /// 添加CSS样式内容
43         /// </summary>
44         /// <param name="page">页面</param>
45         /// <param name="content">样式内容</param> 
46         public void AddStyle(System.Web.UI.Page page, string content)
47         {
48                 HtmlGenericControl styleControl = new HtmlGenericControl("style");
49                 styleControl.Attributes.Add("type", "text/css");
50                 styleControl.InnerHtml = content;
51                 page.Header.Controls.Add(styleControl);
52         }
53 
54         /// <summary>
55         /// 添加Meta标签
56         /// </summary>
57         /// <param name="page">页面</param>
58         /// <param name="name">Meta名字</param>
59         /// <param name="content">Meta内容</param> 
60         public void AddMeta(System.Web.UI.Page page, string name, string content)
61         {
62                 HtmlMeta meta = new HtmlMeta();
63                 meta.Name = name;
64                 meta.Content = content;
65                 page.Header.Controls.Add(meta);
66         }