![](/img/trans.png)
[英]How do I call a javascript file that's embedded in a dll in my asp.net web application?
[英]ASP.Net - How do I include an embedded JavaScript file from another project?
我有兩個項目,一個是控件庫,另一個是我的主項目。 目前,在控件庫中,我正在使用用戶控件和一些嵌入控件庫中的css文件。
通過在用戶控件的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);
我認為以類似的方式包含我的所有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);
現在,CSS都可以正常工作,但是我的JS函數在嘗試調用它們時會拋出Object Required異常。
我要用正確的方法嗎? 還是有更好的方法將嵌入式js文件從另一個程序集中包含到另一個項目中?
似乎還不錯; 但是,在這一點上,我確實會使用客戶端工具來確定是否一切都已到達並被使用(提琴手/即工具欄/螢火蟲/等)。
如果我不得不猜測,我會說您的代碼可以正常工作,但是您使用的任何瀏覽器都會忽略javascript,因為script標簽沒有結束標簽(即<script></script> opposed to <script />);
由於某些原因,某些瀏覽器對此很挑剔
就個人而言,正如其他人所建議的那樣,使用諸如Firefox的FireBug , Fiddler或Internet Explorer的開發人員工具之類的工具來檢查對服務器進行的調用以及它們發回的響應-這就是BigBlondeViking所指的。
我還要檢查您是否已在解決方案中將JS文件標記為“構建”,而不是默認為“不采取任何措施”。
但是,確實存在一種添加嵌入式腳本資源的更干凈的方法,即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.