[英]ASP.Net - How do I include an embedded JavaScript file from 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.
目前,在控件库中,我正在使用用户控件和一些嵌入控件库中的css文件。
I can use the embedded CSS files in my main project by doing the following from my user control's PreRender event: 通过在用户控件的PreRender事件中执行以下操作,可以在主项目中使用嵌入式CSS文件:
// 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: 我认为以类似的方式包含我的所有javascript文件将是有意义的,因此我将嵌入式javascript文件包括在内,以进行以下操作:
// 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. 现在,CSS都可以正常工作,但是我的JS函数在尝试调用它们时会抛出Object Required异常。
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?
还是有更好的方法将嵌入式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 (ie <script></script> opposed to <script />);
如果我不得不猜测,我会说您的代码可以正常工作,但是您使用的任何浏览器都会忽略javascript,因为script标签没有结束标签(即
<script></script> opposed to <script />);
for some reason some browsers are picky about that 由于某些原因,某些浏览器对此很挑剔
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. 就个人而言,正如其他人所建议的那样,使用诸如Firefox的FireBug , Fiddler或Internet Explorer的开发人员工具之类的工具来检查对服务器进行的调用以及它们发回的响应-这就是BigBlondeViking所指的。
I'd also check that you have marked the JS file as "build" in the solution - rather than the default of "take no action". 我还要检查您是否已在解决方案中将JS文件标记为“构建”,而不是默认为“不采取任何措施”。
However, there is indeed a cleaner way of adding embedded script resouces, the ClientScriptManager's " RegisterClientScriptResource " method: 但是,确实存在一种添加嵌入式脚本资源的更干净的方法,即ClientScriptManager的“ RegisterClientScriptResource ”方法:
// 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.