自定义web控件引用外部资源

1、新建web控件项目

2、添加JS文件或者CSS文件

3、设置JS或CSS文件属性中的”生成操作“为”嵌入的资源“

4、在AssemblyInfo.cs中注册资源

     [assembly: WebResource("命名空间.文件夹名.JScript1.js", "text/javascript")]

     WebResource需要引用System.Web.UI;

5、在控件中注册

     一般是protected override void OnPreRender(EventArgs e)方法

     CSS资源

string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(),  
"命名空间.资源文件夹.Stylesheet.css");
HtmlLink link = new HtmlLink();
link.Href = cssUrl;
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(link);

     JS资源

     Page.ClientScript.RegisterClientScriptResource(this.GetType(), "命名空间.文件夹名.JScript1.js");

原文地址:https://www.cnblogs.com/youyuan1980/p/8109886.html