DCSIMG
How to declare a javascript file as an embedded resource in assembly - Wortzel's blog

Wortzel's blog

.Net (2.0, 3.0, 3.5), C#, Asp.net, Com+, GIS(ESRI Software), Management, Analysis & Design, Life, Trips, And more...
How to declare a javascript file as an embedded resource in assembly

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!

Published Wednesday, June 13, 2007 8:34 PM by Avi Wortzel

תגים:, ,

Comments

# re: How to declare a javascript file as an embedded resource in assembly@ Thursday, June 14, 2007 9:48 PM

Great post.

One comment :

The script file name in the GetWebResourceUrl and the WebResource attribute MUST be specified with the full path of the file in the project. That means that if the js file is in a folder within the project, the folder must be specified as well.

For example :

If the alertUtils.js is in the folder Scripts in the MyCustomControl project then the script file name needs to be :

"MyCustomControls.Scripts.alertUtils.js"

YsA

# re: How to declare a javascript file as an embedded resource in assembly@ Wednesday, June 20, 2007 11:09 AM

Hi,

We are planning an event for bloggers and would like to send you an invitation.

Can you please contact me with your email address at http://blogs.microsoft.co.il/blogs/liorz/contact.aspx

Thanks,

Lior Zoref

Microsoft Israel

ליאור צורף

# re: How to declare a javascript file as an embedded resource in assembly@ Thursday, July 17, 2008 9:41 PM

Hi:

I have a question.

I do not want to use any other assembly. I have few javascript files in my presentation tier itself and those are served for different purposes on various screens with in the project.

I want these files to be added as an embedded resources. Is that possible?

Please let me know. Thanks.

Regards,

Shashi

Shashidhar

# re: How to declare a javascript file as an embedded resource in assembly@ Wednesday, July 30, 2008 5:28 PM

Hi Shashi,

I think this post can help you:

www.eggheadcafe.com/.../using-the-webresourceaxd.aspx

:)

Avi Wortzel