There are many scenarios it is recommended to use an embedded resource which is part of the assembly. One of the common cases is having a custom control with custom script file and wanting to ship them in a close assembly to other projects. Thanks to deployments reasons we prefer to ship only the assembly without the other resource files. There is a simple approach to do it in .Net 2.0.
Ok, what do we have to do in order to accomplish this task?
Assumptions:
- We have a custom control which is called "AlertButton"(Inherit from the Button class).
- This control use javascript file which is called "alertUtils.js".
- The control is complied into "MyCustomControls.dll" assembly.
Tasks:
1. Define the javascript file as Embedded resource after the build action event:

2. Add a WebResource definition to the AssemblyInfo.
[assembly: System.Web.UI.WebResource([FileName], [ResourceType])]
For example:
[assembly: System.Web.UI.WebResource("MyCustomControls.alertUtils.js", "application/x-javascript")]
3. Then register this web resource in your web control:
[ToolboxData("<{0}:MyCustomControl runat=server></{0}:MyCustomControl>")]
public class MyCustomControl : Button
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// For pages with ajax extension
ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference
("MyCustomControls.alertUtils.js", " MyCustomControls"));
// For pages without ajax extension
Page.ClientScript.RegisterClientScriptInclude(
this.GetType(), "JavaScriptFileKey",
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"MyCustomControls.alertUtils.js"));
}
}
4. And that is all!