I am currently working on two projects - a control library and my main project. In the control library, I am using a user control and some embedded CSS files. To use these CSS files in my main project, I am adding them from the PreRender event of my user control:
// Register the default CSS resource
string cssIncludeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
string cssIncludeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.WebNotify.WebNotify.css");
LiteralControl cssInclude = new LiteralControl(String.Format(cssIncludeTemplate, cssIncludeLocation));
((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(cssInclude);
Thinking along the same lines, I decided to include my JavaScript files similarly. I added the embedded JavaScript file like this:
// Register the js
string jsIncludeTemplate = "<script type='text/javascript' src='{0}'></script>";
string jsIncludeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.Scripts.MyScript.js");
LiteralControl jsInclude = new LiteralControl(String.Format(jsIncludeTemplate, jsIncludeLocation));
((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(jsInclude);
While the CSS is working fine, I am encountering "Object Required" exceptions when trying to call my JS functions. Am I approaching this the right way? Is there a better method for including an embedded JS file from another assembly into a different project?