如何将 JQuery 嵌入自定义服务器控件
我正在编写一个服务器控件,它是一个带有建议的搜索框(当用户开始键入时,将出现匹配的单词供他们选择)。我正在使用 jquery 和 C#。
当我在正在测试的页面上包含 jquery Lib 时,该控件工作正常。但是,当我尝试将 lib 嵌入到 DLL 中时,它将不起作用。
我已经尝试完全嵌入它并注册脚本:
ClientScriptManager manager = this.Page.ClientScript;
manager.RegisterClientScriptResource(typeof(Search), jqueryResource); //jqueryResource= SearchControl.javascript.jquery.js
并且我还尝试在包含jquery的页面标题中添加一个标签:
string javascriptUrl = manager.GetWebResourceUrl(typeof(Search), jqueryResource);
LiteralControl jquery = new LiteralControl(string.Format(javascriptInclude, jqueryUrl));
Page.Header.Controls.Add(jquery);
当尝试从控件获取信息时,两种方法都会导致javascript错误(预期对象)
var params = $("#searchBox").val(); //called on keyup when typing in textbox
有人完成了吗这个之前?是否可以?有人可以解释一下吗?
PS:程序集已在 AssemblyInfo.cs 文件中注册。
I am writing a server control that is a search box with suggestions (when the user starts typing, matching words will appear for them to select). I am using jquery and C#.
The control works fine when I include the jquery Lib on the page that I am testing on. However, it will not work when I attempt to embed the lib in the DLL.
I have tried completely embedding it and registering the script:
ClientScriptManager manager = this.Page.ClientScript;
manager.RegisterClientScriptResource(typeof(Search), jqueryResource); //jqueryResource= SearchControl.javascript.jquery.js
and I am also tried just adding a tag in the header of the page that includes the jquery:
string javascriptUrl = manager.GetWebResourceUrl(typeof(Search), jqueryResource);
LiteralControl jquery = new LiteralControl(string.Format(javascriptInclude, jqueryUrl));
Page.Header.Controls.Add(jquery);
Both ways cause javascript errors (Object Expected) when trying to get information from controls
var params = $("#searchBox").val(); //called on keyup when typing in textbox
Has someone done this before? Is it possible? Can someone shed some light on this?
PS: Assemblies are already registered in the AssemblyInfo.cs file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:重新阅读你的问题,发现我刚刚重复了你所做的事情。然而,这是我的实现,它按预期工作:
您需要使用
RegisterClientScriptResource
。只需在控件的 PreRender 事件中调用以下代码片段(取自我的 ScriptRegisterHelper 静态类):为了完成,这里是实现:
以及 AssemblyInfo 文件:
EDIT: Just re-read your question and realised I've just repeated what you've done. However this is my implementation which works as expected:
You need to use
RegisterClientScriptResource
. Just call the following snippet (taken from my ScriptRegisterHelper static class) in the PreRender event of the control:And for completion, here's the implentation:
And the AssemblyInfo file: