开发自定义web控件时把css,js嵌入成资源
时间:2010-08-30 来源:loogn
1设置文件的属性:生成操作--》嵌入的资源
2在AssemblyInfo.cs中添加
[assembly: WebResource("程序集名.文件名", "类型")] 如:
[assembly: WebResource("HelloWorld.SH.css","text/css")]
[assembly: WebResource("HelloWorld.SH.js", "text/javascript")]
3重写控件类的OnPreRender,添加引用代码
string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "HelloWorld.SH.css");
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssUrl;
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
this.Page.Header.Controls.Add(cssLink);
this.CssClass = "divHW";
string jsUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "HelloWorld.SH.js");
Page.ClientScript.RegisterClientScriptInclude("SH", jsUrl);