ASP。NET jQuery自动完成-第一次搜索后没有响应的文本框

时间:2021-10-26 03:15:29

So far I'm not finding the same problem in other questions on this site. Here's what I'm experiencing:

到目前为止,我在这个网站上的其他问题中没有发现同样的问题。这就是我经历:

I have an ASP.NET WebForms app with an UpdatePanel containing a search area where I have a ASP:TextBox that I use for a jQuery autocomplete.

我有一个ASP。NET WebForms应用程序的UpdatePanel包含一个搜索区域,其中我有一个ASP:TextBox,用于jQuery自动完成。

$(document).ready(function() {
    $("#tabContainer_tabSearchBreaks_txtSearchName").autocomplete("AutoCompleteEmployee.ashx", { minChars: 3, maxItemsToShow: 10 });
});

This whole thing works fine, but if I click on a ASP:Button and process some code for the search area, the autocomplete javascript no longer works.

这一切都很正常,但是如果我点击一个ASP:按钮并处理一些搜索区域的代码,自动完成的javascript就不再有效了。

Any ideas???

任何想法? ? ?

There's got to be a solution to reset the textbox to call the js code.

必须有一个解决方案来重置文本框,以调用js代码。

[Update - More Code] Here's what the update button does for the search area that is separate from the autocomplete code:

[更新-更多代码]以下是更新按钮对与自动完成代码分离的搜索区域的作用:

 try {
     int employeeID;
     string[] namelst = txtSearchName.Text.Split(new string[] {
         " "
     }, StringSplitOptions.None);
     employeeID = int.Parse(namelst[2].Substring(1, namelst[2].Length - 2));
     string name = namelst[0] + " " + namelst[1];
     var breaks = bh.ListBreaksForEmployeeByDate(employeeID, DateTime.Parse(txtFromDate.Text), txtToDate.Text.Length > 0 ? DateTime.Parse(txtToDate.Text).AddDays(1).AddSeconds(-1) : DateTime.Today.AddDays(1).AddSeconds(-1));

     if (breaks.Count() > 0) {
         lblEmployeeTitle.Text = "Breaks for " + name;
         gridSearchBreaks.DataSource = breaks;
         gridSearchBreaks.DataBind();
     }
 } catch {}

Hope this helps. For the time being I've hidden the tab that contains this problem from the users.

希望这个有帮助。目前,我已经向用户隐藏了包含此问题的选项卡。

2 个解决方案

#1


10  

I think you're experiencing a classic problem with ASP.NET UpdatePanel controls and jQuery. The problem is the following: jQuery code (in your case it's auto-complete, but it can be anything) works fine on the page load, but it stops working after a partial postback. If this is the case, there is a couple of things you need to understand about using jQuery with UpdatePanel controls.

我想你正在经历一个典型的ASP问题。NET UpdatePanel控件和jQuery。问题是:jQuery代码(在您的情况下,它是自动完成的,但它可以是任何东西)在页面负载上运行良好,但是在部分回发之后它就停止工作了。如果是这样的话,您需要了解关于使用UpdatePanel控件使用jQuery的一些事情。

First, all event bindings defined in $(document).ready stop working after the first partial postback (and I assume that your button click causes a partial postback). This is just the way ASP.NET works. So how do you fix it? Well, there are several ways to address this problem. A typical recommendation is to substitute $(document).ready with the ASP.NET AJAX's own pageLoad event. This may solve one problem, but it will most likely cause more issues because now you will be binding events on every partial postback causing repeated execution of event handlers on a single event. You can address some issues by calling unbind for your selector before performing any bindings. For simple event bindings, you can keep using $(document).ready with the live function (instead of click, hover, etc).

首先,在$(document)中定义的所有事件绑定。准备在第一个部分回发之后停止工作(我假设您的按钮单击会导致部分回发)。这就是ASP。净工作。那么如何修复它呢?有几种方法可以解决这个问题。一个典型的建议是替换$(文档)。准备用ASP。NET AJAX自己的pageLoad事件。这可能会解决一个问题,但很可能会导致更多的问题,因为现在您将对每个部分回发绑定事件,从而导致在单个事件上重复执行事件处理程序。在执行任何绑定之前,可以通过调用选择器的unbind来解决一些问题。对于简单的事件绑定,您可以继续使用$(document)。准备使用live函数(而不是单击、悬停等)。

I haven't used jQuery plugins with UpdatePanel, so I can't say for sure what you need to do, but once you understand what is happening, it should not be hard to find the right approach. To learn more about this problem and possible solutions, please read Dave Ward's article $(document).ready() and pageLoad() are not the same! (the article includes several examples).

我还没有使用UpdatePanel的jQuery插件,所以我不能确定您需要做什么,但是一旦您了解了正在发生的事情,就不应该很难找到正确的方法。要了解关于这个问题和可能的解决方案的更多信息,请阅读Dave Ward的文章$(document).ready()和pageLoad()不是一回事!(这篇文章包括几个例子)。

#2


1  

I suggest you use a tool like Firebug, using which you can pinpoint where the error occurs. You can monitor ajax requests, to see if it has actually stopped making requests, if they make it all the way through, and what response you get. You can also use it to debug javascript at runtime, and it often gives you error messages that you might not have noticed otherwise. Sounds like just the tool you need at the moment.

我建议您使用Firebug这样的工具,您可以使用它来确定错误发生的位置。您可以监视ajax请求,看看它是否已经停止发出请求,是否一直发出请求,以及得到的响应。您还可以使用它在运行时调试javascript,它通常会给您一些您可能没有注意到的错误消息。听起来像是你现在需要的工具。

#1


10  

I think you're experiencing a classic problem with ASP.NET UpdatePanel controls and jQuery. The problem is the following: jQuery code (in your case it's auto-complete, but it can be anything) works fine on the page load, but it stops working after a partial postback. If this is the case, there is a couple of things you need to understand about using jQuery with UpdatePanel controls.

我想你正在经历一个典型的ASP问题。NET UpdatePanel控件和jQuery。问题是:jQuery代码(在您的情况下,它是自动完成的,但它可以是任何东西)在页面负载上运行良好,但是在部分回发之后它就停止工作了。如果是这样的话,您需要了解关于使用UpdatePanel控件使用jQuery的一些事情。

First, all event bindings defined in $(document).ready stop working after the first partial postback (and I assume that your button click causes a partial postback). This is just the way ASP.NET works. So how do you fix it? Well, there are several ways to address this problem. A typical recommendation is to substitute $(document).ready with the ASP.NET AJAX's own pageLoad event. This may solve one problem, but it will most likely cause more issues because now you will be binding events on every partial postback causing repeated execution of event handlers on a single event. You can address some issues by calling unbind for your selector before performing any bindings. For simple event bindings, you can keep using $(document).ready with the live function (instead of click, hover, etc).

首先,在$(document)中定义的所有事件绑定。准备在第一个部分回发之后停止工作(我假设您的按钮单击会导致部分回发)。这就是ASP。净工作。那么如何修复它呢?有几种方法可以解决这个问题。一个典型的建议是替换$(文档)。准备用ASP。NET AJAX自己的pageLoad事件。这可能会解决一个问题,但很可能会导致更多的问题,因为现在您将对每个部分回发绑定事件,从而导致在单个事件上重复执行事件处理程序。在执行任何绑定之前,可以通过调用选择器的unbind来解决一些问题。对于简单的事件绑定,您可以继续使用$(document)。准备使用live函数(而不是单击、悬停等)。

I haven't used jQuery plugins with UpdatePanel, so I can't say for sure what you need to do, but once you understand what is happening, it should not be hard to find the right approach. To learn more about this problem and possible solutions, please read Dave Ward's article $(document).ready() and pageLoad() are not the same! (the article includes several examples).

我还没有使用UpdatePanel的jQuery插件,所以我不能确定您需要做什么,但是一旦您了解了正在发生的事情,就不应该很难找到正确的方法。要了解关于这个问题和可能的解决方案的更多信息,请阅读Dave Ward的文章$(document).ready()和pageLoad()不是一回事!(这篇文章包括几个例子)。

#2


1  

I suggest you use a tool like Firebug, using which you can pinpoint where the error occurs. You can monitor ajax requests, to see if it has actually stopped making requests, if they make it all the way through, and what response you get. You can also use it to debug javascript at runtime, and it often gives you error messages that you might not have noticed otherwise. Sounds like just the tool you need at the moment.

我建议您使用Firebug这样的工具,您可以使用它来确定错误发生的位置。您可以监视ajax请求,看看它是否已经停止发出请求,是否一直发出请求,以及得到的响应。您还可以使用它在运行时调试javascript,它通常会给您一些您可能没有注意到的错误消息。听起来像是你现在需要的工具。