Saturday, April 06, 2013

Calling a function after .Net Ajax UpdatePanel Partial Postback

In case you need to call a javascript code blocks after a Partial Postback using Update Panel, you can use the following code block after window load.

 

var v= Sys.WebForms.PageRequestManager.getInstance()

 

v.add_endRequest( function ()

          {

               // callback code goes here

          }

     );

 

The callback code will be called after a Partial PostBack. In case the callback method should also be called during first time page load, you can move the callback code to a method and call it in two places as mentioned below

 

var v= Sys.WebForms.PageRequestManager.getInstance()

MyCallBackCode();

v.add_endRequest( function ()

          {

               MyCallBackCode();

          }

     );

 

function MyCallBackCode()

{

// callback code goes here

}

 

To execute this on window load you can use jQuery

 

$(document).ready(function(){

     var v= Sys.WebForms.PageRequestManager.getInstance()

     MyCallBackCode();

     v.add_endRequest( function ()

               {

                   MyCallBackCode();

               }

          );

 

     function MyCallBackCode()

     {

          // callback code goes here

     }

});