views:

112

answers:

3

I have a custom control that has a webresource in it.

The webresource is a javascript file and I have the build option on the javascript file set to "Embedded Resource" and I have the following lines of code in my AssemblyInfo.cs for the project my custom control is in:

// Export the MultiSelectGridView.js file
[assembly: WebResource("SOM.DCO.CustomWebControls.MultiSelectGridView.js", "application/x-javascript")]

In my custom control, I have the following lines in the overriden onload event:

private const string MULTISELECTGRIDVIEW_JS = "SOM.DCO.CustomWebControls.MultiSelectGridView.js";
Type t = this.GetType();
                string url = Page.ClientScript.GetWebResourceUrl(t, MULTISELECTGRIDVIEW_JS);
                if (!Page.ClientScript.IsClientScriptIncludeRegistered(t, MULTISELECTGRIDVIEW_JS))
                    Page.ClientScript.RegisterClientScriptInclude(t, MULTISELECTGRIDVIEW_JS, url);

I've also tried the following:

private const string MULTISELECTGRIDVIEW_JS = "SOM.DCO.CustomWebControls.MultiSelectGridView.js";
if (!Page.ClientScript.IsClientScriptIncludeRegistered(t, MULTISELECTGRIDVIEW_JS))
                    Page.ClientScript.RegisterClientScriptResource(t, MULTISELECTGRIDVIEW_JS);

Unfortunately, no matter what I do, the webresource that I get back is blank, and so none of the functions in my javascript file are included/defined in my consuming page.

Am I missing something obvious?

+1  A: 

The only thing I've had an issue with in the past is passing the wrong Type when calling GetWebResourceUrl. You have to have the Type be a Type that comes from the same Assembly as the Embedded Resource.

Using this.GetType() can present problems embedding resources (as described by Rick Strahl)

Everything else looks ok to me.

Gordon Tucker
A: 

Well it turns out that I even though my javascript was in the "namespace" SOM.DCO.CustomWebControls, it was inside a folder called "MultiSelectGridView". So I changed all the references to it from SOM.DCO.CustomWebControls.MultiSelectGridView.js to SOM.DCO.CustomWebControls.MultiSelectGridView.MultiSelectGridView.js and now it works.

Amanda Myer
A: 

I just had this same issue and it was a nightmare to figure out the fix. I finally figured it out, thanks to this link.

If you right-click on your solution name in Solution Explorer, and choose "properties", you'll see a text box for "Default namespace".

This default namespace value must be the same as the namespace you're putting in front of your web resource.

So, if the default namesspace in your properties is only "SOM" or only "CustomWebControls", you need to change it to "SOM.DCO.CustomWebControls".

Then use that same full namespace in your "assembly: WebResource" and your call to RegisterClientScriptInclude (or RegisterClientScriptResource, or whatever method you're using to attach the web resource.

Kristopher P.