views:

4704

answers:

4

Is there a way to execute script when an UpdatePanel process is finished.

I have a page that allows "inserting", "copying", and "editing" of a record. This is all done on an UpdatePanel to prevent a page navigation. Somewhere else on the page I would like to print a "flash" message - like "You have successfully entered a record." It is not near the UpdatePanel and I'd like to use a jQuery effect on the message so it fades out after 4 seconds. How can I send script back with the UpdatePanel or have it execute after a UpdatePanel refresh? Should I write script to an asp:literal? thoughts?

+7  A: 

Yes:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

And then:

function endRequestHandler(sender, args)
{
  // Do stuff
}

Documentation here and here. Keep in mind that this will fire for every AJAX request on the page.

Robert C. Barth
+3  A: 

This should do the trick:

       <script type="text/javascript">
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(BeginRequestHandler);
            prm.add_endRequest(EndRequestHandler);

            function BeginRequestHandler(sender, args) 
            {
                 //Jquery Call
            }

            function EndRequestHandler(sender, args) 
            {
                 //Jquery Call

            }
        </script>
cgreeno
+1  A: 

Bruno,

First, to answer your direct question. In your callback that is being called by the update panel, you should be able to use a RegisterStartupScript call to invoke a JS method . Then in your JS method, you would show the message and then you can use do a:

setTimeout('$('#myMessage').fadeOut("slow")', 4000);

to have it fade away after 4 seconds.

To go one step further, since you're already implementing JavaScript, I would invite you to check out this article about UpdatePanels. If possible, I would try to send Ajax calls to do your inserting, copying, and editing and this would further simplify your user feedback and would prevent excess info across the wire.

AdamB
I don't believe calls to RegisterStartupScript can inject JS into an AJAX callback.
Robert C. Barth
I am moving away from UpdatePanels and moving towards WCF/AJAX via MS AJAX and jQuery. This is just an older projects that works, but I thought I would add a few UpdatePanels to make it slicker.
tyndall
+1  A: 

Here is an article for how to do it using ScriptManager's static method RegisterClientScriptBlock. Tried it and works like a charm.

http://csharperimage.jeremylikness.com/2009/06/inject-dynamic-javascript-into-aspnet.html

Matthew