要想在自定义用户控件中嵌入资源,从以下几个步骤入手:
1.在AssemblyInfo.cs中注册资源,文件夹层级用点隔开。例如:
[assembly: System.Web.UI.WebResource("FirsteLite.OMS.Checking.UserControls.test.jpg", "image/jpg")]
2.修改资源文件的"生成操作"的值为“嵌入的资源"。
3.在代码里引用的时候可以用Page.ClientScript.GetWebResourceUrl来获取访问地址。例如:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace FirsteLite.OMS.Checking.UserControls { public class MarkingButton : System.Web.UI.WebControls.LinkButton { protected override void OnPreRender(EventArgs e) { this.Style.Add("background-image", Page.ClientScript.GetWebResourceUrl(this.GetType(), "FirsteLite.OMS.Checking.UserControls.test.jpg")); base.OnPreRender(e); } } }
4.如果是js可能还要在页面动态引入。例如:
protected override void OnPreRender(EventArgs e) { Page.ClientScript.RegisterClientScriptInclude("WdatePicker", GetResUrl("WdatePicker.js")); base.OnPreRender(e); }