Executing JavaScript function after update panel has updated

Hi,

Last week I was working on a project and integrating AJAX to some existing code base. I had two small requirements. I wanted to run different JavaScript function once the server side execution is finished.

I had one function which was needed to fire after and or every update panel update, and some other JavaScript function to fire on specific update of update panel.

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.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 1/17/2008 2:57:36 PM

hey, thank you. that was extremely heplful, and very useful for when you need to run different javascript for different steps within a wizard control. thanks again!!

Posted on 3/11/2008 11:46:22 AM

This is not working for me; however, my situation is slightly different. From an async postback I I programatically add a UserControl to an update panel. The UserControl has some javascript inside it that needs to run immeditately to hook events and such. The same user control works fine when placed on a page declaratively -- all the JS runs on load. Any idea how I can get this JS to run at the end of the async request?

Posted on 4/8/2008 7:04:46 AM

Hey Thanks a lot!! was exactly looking for this..

Posted on 4/25/2008 2:46:52 PM

Thank you sooooo much for posting this. I have tried everyway to get this working. Your method finally worked for me....What did we ever do before the internet....Nice work.

Posted on 4/29/2008 10:06:06 PM

but does it run always?i think that it is only fired at the first loading of the page but not after a call back.
also my javascript code was dynamic and i use the alert with some dynamic string.

Posted on 6/27/2008 6:39:12 AM

how to validate a single textbox with many Regex in MVC and how to implement callback in MVC

Posted on 7/30/2008 2:53:31 AM

Hi,

I am calling a function like this,
ScriptManager.RegisterStartupScript(this, this.GetType(), "foo", "foo(a);", true);

but at this line it is throwing error saying that a is undefined which is the parameter I am trying to pass.
a is the anchor element for which I want to make some changes.

Please let me know how to do this. Thank you.

Posted on 8/25/2008 8:05:18 AM

lakshmi,

put the a between single quotes....
ScriptManager.RegisterStartupScript(this, this.GetType(), "foo", "foo('a');", true);

:-)

Posted on 10/14/2008 9:09:47 AM

Hi, this post where weryyyy useful but can you tell me how to register function to be fired BEFORE request?
Can you help me with this?

Posted on 10/14/2008 9:10:21 AM

Hi, this post where weryyyy useful but can you tell me how to register function to be fired BEFORE request?
Can you help me with this?

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia