Like it

Monday, May 3, 2010

Run Javascript after UpdatePanel update

To execute a JavaScript function after any and every update panel update we can use the following code.

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(foo);
This will make the JavaScript function foo() to fire after every update panel update.
But if you want to call specific JavaScript function after specific update panel update, then this method will not be useful. What we can do is call the JavaScript function from the server side.

This can be done with the help of the static function RegisterStartupScript of the scriptmanager class. The following code needs to be written in the server side for the execution of JavaScript function after a call back. This code should be written in the event which is executed in the callback (button click).

ScriptManager.RegisterStartupScript(this, this.GetType(), "foo", "foo();", true);
Here the code will execute the JavaScript function foo() after the server side execution is complete.