简体   繁体   English

ASP.Net-如何包含另一个项目的嵌入式JavaScript文件?

[英]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的FireBugFiddler或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.

相关问题 我如何在asp.net Web应用程序中调用嵌入dll中的javascript文件? - How do I call a javascript file that's embedded in a dll in my asp.net web application? 如何使用 Z686155AF75A60A0EDD3F6E9D80C1 从 ASP.NET Controller 重定向到另一个 web 页面? - How do I redirect to another web page from an ASP.NET Controller using JavaScript? 如何在JavaScript中访问嵌入式ASP.NET GlobalResources? - How can I access embedded ASP.NET GlobalResources in JavaScript? 如何将XML文件作为vNext(ASP.NET 5)类库中的嵌入式资源? - How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library? 如何在asp.net中的CS代码中包含javascript文件 - How to include javascript file in the cs code behind in asp.net 包括asp.net填充的javascript文件 - include asp.net filled javascript file 如何从asp:Button的OnCommand中的另一个文件调用方法? asp.net C# - How do I call a Method from another file in OnCommand of my asp:Button? asp.net C# 如何在WCF asp.net中的另一个项目中引用输入字段? - How do I reference an input field in another project in WCF asp.net? 将文件传递到ASP.NET中的另一个项目 - Pass file to another project in ASP.NET 如何在ASP.Net应用程序作为电子邮件发送的短信中包含换行符? - How do I include a newline in a text message sent as email from an ASP.Net application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM