c# - ASP.Net - How do I include an embedded JavaScript file from another project? - Stack Overflow

I've got two projects, one is a control library and another is my main project. From the control l

I've got two projects, one is a control library and another is my main project. From the control library I am currently using a user control and some css files which are embedded in the control library.

I can use the embedded CSS files in my main project by doing the following from my user control's PreRender event:

  // 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 make sense to include all my javascript files in a similar fashion, so I included the embedded javascript file 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);

Now, the CSS all works perfectly, however my JS functions throw Object Required exceptions when trying to call them.

Am I going about this the correct way? Or is there a better way of including an embedded js file from another assembly into another project?

I've got two projects, one is a control library and another is my main project. From the control library I am currently using a user control and some css files which are embedded in the control library.

I can use the embedded CSS files in my main project by doing the following from my user control's PreRender event:

  // 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 make sense to include all my javascript files in a similar fashion, so I included the embedded javascript file 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);

Now, the CSS all works perfectly, however my JS functions throw Object Required exceptions when trying to call them.

Am I going about this the correct way? Or is there a better way of including an embedded js file from another assembly into another project?

Share Improve this question edited Aug 24, 2009 at 8:04 djdd87 asked Aug 21, 2009 at 15:47 djdd87djdd87 68.5k29 gold badges159 silver badges197 bronze badges 3
  • 1 what does the request for the js file response status code look like? 404? 500? – BigBlondeViking Commented Aug 21, 2009 at 15:57
  • Sorry, what do you mean? – djdd87 Commented Aug 21, 2009 at 15:59
  • If you're running Firefox, install FireBug (getfirebug.), browse to your site, and on the Script tab of FireBug, check what the response and output of your script calls are. If you're running IE8, press F12 to bring up the Developer Tools, and check the script there. – Zhaph - Ben Duguid Commented Aug 24, 2009 at 10:46
Add a ment  | 

2 Answers 2

Reset to default 3 +100

Personnally, as others have suggested, use some tools such as FireBug for Firefox, Fiddler, or the Developer Tools for Internet Explorer to check what calls are being made to your servers, and what responses they are sending back - that's what BigBlondeViking's referring to.

I'd also check that you have marked the JS file as "build" in the solution - rather than the default of "take no action".

However, there is indeed a cleaner way of adding embedded script resouces, the ClientScriptManager's "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");

Seems fine; however, at this point I'd really be using client tools to determine whether or not everything's getting there and being used (fiddler/ie toolbar/firebug/etc).

If I had to guess, I would say your code is working, but whatever browser you're using is ignoring the javascript due to the script tag not having a closing tag (i.e. <script></script> opposed to <script />); for some reason some browsers are picky about that

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745142210a4613481.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信