ASP.Net. How can I include an embedded JavaScript file from another project?

I have two projects, one is a management library and the other is my main project. From the control library, I am currently using a custom control and some css files embedded in the control library.

I can use inline CSS files in my main project by doing the following from my PreRender custom element:

  // Register the default CSS resource
  string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
  string includeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.WebNotify.WebNotify.css");
  LiteralControl cssInclude = new LiteralControl(String.Format(includeTemplate, includeLocation));
  ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(cssInclude);

      

I thought it would then be wise to include all of my javascript files in a similar way, so I included the inline javascript file by doing the following:

  // Register the js
  string includeTemplate = "<script type='text/javascript' src='{0}'></script>";
  string includeLocation = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyCompany.ControlLibrary.Scripts.MyScript.js");
  LiteralControl jsInclude = new LiteralControl(String.Format(includeTemplate, includeLocation));
  ((System.Web.UI.HtmlControls.HtmlHead)Page.Header).Controls.Add(jsInclude);

      

The CSS is working fine now, however my JS functions are throwing Object Required exceptions when I try to throw them.

Am I correct about this? Or is there a better way to include an inline js file from another assembly in another project?

+2


source to share


2 answers


Typically, as others have suggested, use some tools like FireBug for Firefox, Fiddler or Developer Tools for Internet Explorer to check what calls are being made to your servers and what responses they send back - which BigBlondeViking refers to.

I would also check that you mark the JS file as "assembly" in the solution and not the default "take no action".



However, there really is a cleaner way of adding inline script resouces, the ClientScriptManager " RegisterClientScriptResource " method:

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Register the client resource with the page.
cs.RegisterClientScriptResource(rstype, 
     "MyCompany.ControlLibrary.Scripts.MyScript.js");

      

+3


source


Seems lovely; however at this point I would actually use the client tools to determine if everything is available and in use (script / i.e. toolbar / firebug / etc).



If I had to guess, I would say that your code works, but whatever browser you are using, ignoring javascript due to the script tag having no closing tag (i.e. <script></script> opposed to <script />);

for some reason some browsers are picky about Besides that

+3


source







All Articles