I was able to fix this problem through two different approaches and learned some things along the way. Note: The steps I performed to add the project file and set it as an Embedded Resource are the same (though I did have to change the file location as outlined below).
JavaScript File Reference
First, to get the JavaScript file reference to work correctly, it appears that the JavaScript files have to either be in the root of the project directory or in a folder named Resources. Since my project structure mimics that of the 12 hive, I had tried to put it under [namespace].12.Template.Layouts, but I couldn't get the subfolder pathing/references to work properly (anyone know how to do this?).
So, in the example my assembly reference now looks like this:
[
assembly: WebResource(@"ProjectNamespace.Resources.Common.js", @"text/javascript")]
Secondly, I modified my web part base class in the override for CreateChildControls to include the following:
ClientScriptManager manager = Page.ClientScript;if (!manager.IsClientScriptIncludeRegistered(@"WebPartScript"))
{
string url = manager.GetWebResourceUrl(this.GetType(), @"ProjectNamespace.Resources.Common.js");manager.RegisterClientScriptInclude(this.GetType(), @"WebPartScript", ResolveClientUrl(url));
}
JavaScript inline method:
The other way (my fallback, since I discoverd this way first
) was to inline the script.
First, I included the same assembly reference (above) to the script:
[assembly: WebResource(@"ProjectNamespace.Resources.Common.js", @"text/javascript")]
Second, I read the script from the embedded resource and registered the block as a script block on the page:
String javaScript = string.Empty;
using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(@"ProjectNamespace.Resources.Common.js"))using (StreamReader reader = new StreamReader(stream))
javaScript = reader.ReadToEnd();
Page.ClientScript.RegisterClientScriptBlock(javaScript.GetType(),
@"ProjectNamespace.Resources.Common.js", javaScript, true);
The real key is to make sure you get the namespace + file name correct, since it's very hard to debug if this is wrong (I only got runtime JS errors in IE for, "Object expected" when the JS runtime couldn't locate the function).
Hope this helps someone else out, because it was driving me crazy!
Jon