我正在从头开始创建一个新项目。我在这个Web应用程序中有我的Web应用程序,我将引用设置为多个类库。
在其中一个库中,我有一些常规的Javascript文件(比如jQuery),我需要在我的Web应用程序的主表中加载。我似乎无法使用Javascript。
如何从我的Web应用程序项目中访问位于不同类库中的Javascript?
附件是一个屏幕截图,以便更清晰。
更新 有没有办法在不使用ScriptManager的情况下实现这一目标?
答案 0 :(得分:1)
如果您希望不再使用ScriptManager但仍保留在DLL中存储库的功能,请尝试以下操作:
我列举了一个如何实现这一目标的简短例子。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
namespace Mri.Controls
{
public class ScriptLoader : Control
{
protected List<string> ScriptUrls;
public ScriptLoader()
{
ScriptUrls = new List<string>();
}
// Have to add libraries here because cannot access the Page object from the Constructor
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
AddScriptKey("Mri.Controls.Resources.Scripts.Libraries.jQuery.js");
}
public void AddScriptKey(string key)
{
// Using the assembly location, find the WebResourceUrl
var webResourceUrl = Page.ClientScript.GetWebResourceUrl(typeof(ScriptLoader), key);
AddScriptUrl(webResourceUrl);
}
public void AddScriptUrl(string url)
{
// Check to see if script already exists
if (!ScriptUrls.Any(s => s.Equals(url)))
ScriptUrls.Add(url);
}
protected override void Render(HtmlTextWriter writer)
{
// Render the script tags
foreach (var scriptUrl in ScriptUrls)
{
writer.Write(string.Format("\n<script type=\"text/javascript\" src=\"{0}\"></script>", scriptUrl));
}
}
}
}
<pages>
<controls>
<add tagPrefix="mri" namespace="Mri.Controls" assembly="Mri.Controls"/>
</controls>
</pages>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page Title</title>
<mri:ScriptLoader id="scriptLoader" runat="server" />
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="cphBody" runat="server" />
</form>
</body>
</html>
我希望这与您所寻找的一致。
答案 1 :(得分:0)
通常,程序集中嵌入的资源由此程序集中的控件使用,通过RegisterClientScript注册,然后通过WebResource.axd处理程序提供给客户端
如果你想从程序集中“提取”资源并对其执行某些操作,请使用Assembly.GetManifestResourceStream()