Welcome to MSDN Blogs Sign in | Join | Help

SYSK 330: How To Use An Embedded Resource From A Sub-Folder

If you’re creating a custom client-side control by deriving from System.Web.UI.IScriptControl, you may want to embed the .js files into your assembly instead of having to deploy them to every web site that uses those controls (this is especially valuable if you’re developing a control library).  All you need to do is to:

  1. Click on the .js file in the Solution Explore to select it, and then set Build Action (in the properties window) to Embedded Resource.

 

  1. Add the following attribute to your control code-behind file (e.g. MyControl.cs):

 

[assembly: System.Web.UI.WebResource("ControlLibrary.MyControl.js", "text/javascript")]

 

  1. In GetScriptReferences, instead of setting the Path property, use the parameterized constructor as follows:

 

public IEnumerable<ScriptReference> GetScriptReferences()
{
     return new ScriptReference[] { new ScriptReference("
ControlLibrary.MyControl.js", "ControlLibrary") };
}

 

However, if you place your JavaScript files in a subfolder (e.g. \ControlLibrary\Scripts\MyControl.js), then you will have to add the folder name to the dotted resource name above, e.g. ControlLibrary.Scripts.MyControl.js.

 

If you don’t add the folder name, you’ll probably see a run-time error message like this:

System.InvalidOperationException: Assembly ControlLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' contains a Web resource with name 'ControlLibrary.MyControl.js', but does not contain an embedded resource with name 'ControlLibrary.MyControl.js'.

 

 

Published Tuesday, April 17, 2007 4:32 AM by irenak

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Assembly X.contains a Web resource with name

Monday, April 30, 2007 12:17 AM by Wander Mahet

Dude, in my case I was receiving an error

"...contains a Web resource with name ...but does not contain an embedded resource with name ..."

and the solutions was tricky but easy.

it's quite simple, in Visual Studio 2005 just right-click the .js file and choose Properties.

On property window, locate Build Action attribute and set it to "Embedded Resource". The problem was happining because it was accidentally to "Compile" (not by me *LOL*).

My folders are:

- MyControlToolkit (c# project)

  - HelloExtender (folder)

      - HelloBehavior.js (file)

      - HelloDesigner.cs (file)

      - HelloExtender.cs (file)

Below a piece of my source code on HelloExtender.cs:

using System;

using System.Web.UI.WebControls;

using System.Web.UI;

using System.ComponentModel;

using System.ComponentModel.Design;

using AjaxControlToolkit;

[assembly: System.Web.UI.WebResource("MyControlToolkit.HelloExtender.OlaBehavior.js", "text/javascript")]

namespace MyControlToolkit

{

   [Description("Show hello message to user")]

   [Designer(typeof(HelloDesigner))]

   [ClientScriptResource("MyControlToolkit.HelloBehavior", "StarjobsControlToolkit.HelloExtender.HelloBehavior.js")]

   [TargetControlType(typeof(Control))]

   [RequiredScript(typeof(CommonToolkitScripts), 0)]

   public class OlaExtender : ExtenderControlBase {

       // TODO: Add your property accessors here.

       //

       [ExtenderControlProperty]

       [DefaultValue("")]

       public string MyProperty

       {

           get

           {

               return GetPropertyValue("MyProperty", "");

           }

           set

           {

               SetPropertyValue("MyProperty", value);

           }

       }

   }

I hope this tip help many others over the world!

cheers,

Wander Mahet

Rio de Janeiro, Brazil

# re: SYSK 330: How To Use An Embedded Resource From A Sub-Folder

Thursday, May 08, 2008 3:20 AM by Anonymous

nice one, wander!

solved my problem perfectly.

cheers from taiwan

# re: SYSK 330: How To Use An Embedded Resource From A Sub-Folder

Thursday, May 14, 2009 7:23 AM by Tom, UK

Thank you! No one else bothered to mention this...

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker