问题
It's a common problem that jQuery widgets inside ASP UpdatePanels need to reinitialized after an ajax request, however my issue is that events are not unbound prior to replacing the HTML inside the UpdatePanel. jQuery takes care of this problem for you whenever you call .html("...") or .remove(), but the UpdatePanel doesn't utilize jQuery.
I have popup elements (inside of the UpdatePanel), which need to be hidden when the user clicks outside of them so I made a jQuery Special Event for detecting when this happens however the Teardown function is never called after the Ajax request. So over time there are more and more event handlers on elements which are no longer attached to the document.
I could have my widget initialization function (which gets ran after every Ajax request), check for detached elements and unbind their events but I'm afraid that could cause other issues down the road.
回答1:
Although I solved the issue with my previous answer, this article on Code Project may be of help of to others with similar issues: http://www.codeproject.com/Articles/34348/jQuery-Memory-Leak-in-UpdatePanel
First it suggests using Sys.Application.add_load
which is ran on pageload and after ASP Ajax requests although it won't solve your memory leak.
Secondly, be careful with how you bind events via javascript closures. However this may not completely solve the the UpdatePanel specific issues.
Third, you can add a dispose
function to HTML elements which is executed when ASP destroys the HTML with the ajax request. The dispose function should unbind event handlers any remove any other references to the element. This should be the final nail in the coffin for ASP UpdatePanel memory leaks.
回答2:
There seems to be a simple solution to this, which is to bind the "clickout" event when the popup is shown and unbind it when the popup is hidden. This still leaves potential for a single event to not be unbound due to an ajax request but that is tolerable.
来源:https://stackoverflow.com/questions/15913738/unbind-events-in-asp-updatepanel-before-ajax-request-to-prevent-memory-leak