What
If you are using jQuery in your application then you may be familiar withdocument.ready() function of jQuery. This function executes after all the elements are loaded in the page. So it is a better way to bind the events for elements or to write some initialization Javascript which we want to be called on page load inside this function. But if you are using updatepanel in your application and doing asynchronous post back you will find that the events are not binded with the elements nor the initialization Javascript code is executed after each asynchronous postback.
Why
Sometimes it happens as the events we are binding or the initialization javascript codes we are writing in side the document.ready() function are not getting called for asynchronous postbacks . This is due to the reason that for asynchronous postback the page is partially loaded and not loaded completely. And it gives rise to some functionality inefficiency.
How To resolve this issue ::
- In its recent version, jquery 1.3.2 offers one live() method which is opposite of die() functionality which bind the event after asynchronous postbacks. So if you want to bind events to the elements you can use live() method instead of bind() method as follows.
- In another approach asp.net offers one method pageload() for javascript through which we can bind the events or execute any code on each page load. Also using this function we can track the partial postback state in javascript.
function pageLoad(sender, args) { $(document).ready(function() { $("#").bind("mouseover", function() { $("#resultPageLoad").append("I wiil be binded in each page load"); }); }); if (args.get_isPartialLoad()) // executes the code after asynchronous postbacks. { alert("Partial Postback occur"); } else // for postback { alert("Full page post back."); } } These are two ways from many through which we can resolve these issue. First case we can only be able to bind the events with elements for asynchronous postbacks but we can't write any custom javascript code which will be executed after asynchronous postbacks. But in the second case it will handle both issues. // with live function $(document).ready(function() { $("#").live("mouseover", function() { $("#resultLive").append("I will always be functional"); }); alert("live") });
Here is the complete page which will help you evaluate these functionality.
//with bind function $(document).ready(function(){ $("#").bind("mouseover", function() { $("#result").append("I Will be there untill postback occurs"); }); alert("bind") }); // with live function $(document).ready(function() { $("#").live("mouseover", function() { $("#resultLive").append("I will always be functional"); }); alert("live") }); function pageLoad(sender, args) { $(document).ready(function() { $("#").bind("mouseover", function() { $("#resultPageLoad").append("I wiil be binded in each page load"); }); }); if (args.get_isPartialLoad()) { alert("Partial Postback occur"); } else { alert("Full page post back."); } }
jQuery bind() | jQuery live() | PageLoad(sender,args) |
---|---|---|