如何将 JQuery 嵌入自定义服务器控件

发布于 2024-08-14 14:55:23 字数 874 浏览 4 评论 0原文

我正在编写一个服务器控件,它是一个带有建议的搜索框(当用户开始键入时,将出现匹配的单词供他们选择)。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-08-21 14:55:23

编辑:重新阅读你的问题,发现我刚刚重复了你所做的事情。然而,这是我的实现,它按预期工作:

您需要使用RegisterClientScriptResource。只需在控件的 PreRender 事件中调用以下代码片段(取自我的 ScriptRegisterHelper 静态类):

/// <summary>
/// Registers the jQuery client script to the page.
/// </summary>
/// <param name="cs">The ClientScriptManager to assign the script to.</param>
internal static void RegisterJQuery(ClientScriptManager cs)
{
    cs.RegisterClientScriptResource(typeof(ScriptRegisterHelper), 
        "MyProject.Resources.jquery.js");
}

为了完成,这里是实现:

protected override void OnPreRender(EventArgs e)
{
    if (!this.DesignMode)
    {

       // Register the JavaScript libraries
       ClientScriptManager cs = this.Page.ClientScript;
       ScriptRegisterHelper.RegisterJQuery(cs);

    }
}

以及 AssemblyInfo 文件:

[assembly: System.Web.UI.WebResource("MyProject.Resources.jquery.js", "text/javascript")]

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:

/// <summary>
/// Registers the jQuery client script to the page.
/// </summary>
/// <param name="cs">The ClientScriptManager to assign the script to.</param>
internal static void RegisterJQuery(ClientScriptManager cs)
{
    cs.RegisterClientScriptResource(typeof(ScriptRegisterHelper), 
        "MyProject.Resources.jquery.js");
}

And for completion, here's the implentation:

protected override void OnPreRender(EventArgs e)
{
    if (!this.DesignMode)
    {

       // Register the JavaScript libraries
       ClientScriptManager cs = this.Page.ClientScript;
       ScriptRegisterHelper.RegisterJQuery(cs);

    }
}

And the AssemblyInfo file:

[assembly: System.Web.UI.WebResource("MyProject.Resources.jquery.js", "text/javascript")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文