前几天在给web控件加资源文件的时候晕死我了,怎么也加不进去,后来看了一篇文章恍然大悟,流程如下
1.把资源文件加入到项目下
2.点文件,属性,生成操作改成嵌入的资源
3.在AssemblyInfo.cs 里加入引用如
[assembly: System.Web.UI.WebResource("Controls.image.icon_page_end.gif", "image/gif")]
这里Controls是项目的命名空间,image是目录,后面是文件名,第二个参数是文件类型
4.用到资源的地方写成这样
imgbtnPre.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Controls.image.icon_page_end.gif");
另外像JS文件应该在PreRender事件中注入
1
// 注册所需脚本
2
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "Tip"))
3
{
4
this.Page.ClientScript.RegisterClientScriptInclude
5
(
6
this.GetType(),
7
"Tip",
8
this.Page.ClientScript.GetWebResourceUrl
9
(this.GetType(), "Controls.Js.Check.js")
10
);
11
}
12![]()
13
// 注册所需样式
14
System.Web.UI.HtmlControls.HtmlLink link = new System.Web.UI.HtmlControls.HtmlLink();
15
link.Attributes["type"] = "text/css";
16
link.Attributes["rel"] = "stylesheet";
17
link.Attributes["href"] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Controls.CSS.Controls.css");
18
this.Page.Header.Controls.Add(link);
// 注册所需脚本2
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "Tip"))3
{4
this.Page.ClientScript.RegisterClientScriptInclude5
(6
this.GetType(),7
"Tip",8
this.Page.ClientScript.GetWebResourceUrl9
(this.GetType(), "Controls.Js.Check.js")10
);11
}12

13
// 注册所需样式14
System.Web.UI.HtmlControls.HtmlLink link = new System.Web.UI.HtmlControls.HtmlLink();15
link.Attributes["type"] = "text/css";16
link.Attributes["rel"] = "stylesheet";17
link.Attributes["href"] = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Controls.CSS.Controls.css");18
this.Page.Header.Controls.Add(link);


浙公网安备 33010602011771号