最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论