Chr*_*s S 9 asp.net jquery webresource embedded-resource
我有一个ASP.NET服务器控件,它依赖于JQuery来实现某些功能.我试图添加为webresource.
我的问题是我的方法包括jquery文件将它添加到正文,或者表格是准确的:
this.Page.ClientScript.RegisterClientScriptInclude(...)
Run Code Online (Sandbox Code Playgroud)
替代方法是将其添加为head标记中的文字:
LiteralControl include = new LiteralControl(jslink);
this.Page.Header.Controls.Add(include);
Run Code Online (Sandbox Code Playgroud)
然而,问题在于头部中使用JQuery失败的任何现有代码srcs,因为之后加载了JQuery(ASP.NET在控件树的底部添加了文字).
是否有一种将JQuery作为嵌入式资源的实用方法,但首先加载到头部?或者我现在应该放弃.
如果要打包jQuery并将其嵌入到自己的服务器控件中,则应使用ScriptManager将其提供给客户端.从头到尾你必须:
在AssemblyInfo.cs中为您的控件添加
[assembly: WebResource("<Your Server Control namespace>.jQuery.js", "application/x-javascript")]
Run Code Online (Sandbox Code Playgroud)使您的控件继承自 System.Web.UI.ScriptControl(或至少实现IScriptControl)
覆盖GetScriptReferences:
protected override IEnumerable<ScriptReference>
GetScriptReferences()
{
return new ScriptReference[] {
new ScriptReference("<Your Server Control namespace>.jQuery.js", this.GetType().Assembly.FullName),
};
}
Run Code Online (Sandbox Code Playgroud)您应该在里面设置所有自己的客户端脚本:
protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
Run Code Online (Sandbox Code Playgroud)
然后,这将确保正确的依赖顺序(即jQuery将可用于您自己的客户端脚本).
更新:
\n\n一种更简单的方法是简单地在脚本中动态添加脚本标签并指向谷歌代码托管。例如
\n\nfunction include_dom(script_filename) {\n var html_doc = document.getElementsByTagName(\'head\').item(0);\n var js = document.createElement(\'script\');\n js.setAttribute(\'language\', \'javascript\');\n js.setAttribute(\'type\', \'text/javascript\');\n js.setAttribute(\'src\', script_filename);\n html_doc.appendChild(js);\n return false;\n}\n\ninclude_dom("http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js");\nRun Code Online (Sandbox Code Playgroud)\n\n该函数取自这篇文章
\n\nCrecentfresh把我推向了正确的方向,我还发现
\n\n\n\n我的问题仍然存在,ScriptManager 在头部的脚本后面添加了引用,但我认为这是一个无法解决的问题。我选择回答自己,但也投票赞成新月新鲜。
\n