.NET Partial Postback in Update Panel Breaks jQuery Autocomplete
If you’ve never had the pleasure of working jQuery, than you need to stop what you are doing and go check it out. Its a cool little JavaScript library that makes throwing some AJAX into your site a snap.
I’ve used jQuery Autocomplete before, and it generally looks something like this:
<script language=”javascript” >
$().ready(function() {
$(”.CLASSNAME”).autocomplete(JobTitles);
$(”#clear”).click(function() {
$(”:input”).unautocomplete();
});
});
</script>
Basically it takes my text box that i have set as class=”CLASSNAME” and in this example a javascript array called JOBTITLES and basically ’suggests’ values that match what they’ve type in so far. Very very handy tool.
I implemented it in a recent project where i was searching a list of names so people could easily search and select the name to add. I had this all wrapped up in an update panel that actually resides in a web user control and had multiple instances of it due to it being printed out in a repeated. Everything worked great….
Then i noticed after i signed up one of the names, my auto-complete broke, WTF? Turns out the update panel does a partial postback that breaks the jQuery. You can find out more information about that here and here.
So in the end i updated my code to look like this:
<script type=”text/javascript” language=”javascript” >
function pageLoad() {
$(”.CLASSNAME”).autocomplete(JOBTITLES);$(”#clear”).click(function() {
$(”:input”).unautocomplete();
});
}
</script>
… and that did the trick!
Also found this article: $(document).ready() and pageLoad() are not the same!