You need hard work to get Lucky!

They are ill discoverers that think there is no land, when they can see nothing but sea.

How to call client-side Javascript function after an UpdatePanel asychronous (Ajax) request is over

Posted by zeemalik on November 27, 2007

If you are using AJAX then the only way i have found yet to give an alert to a user on return to the Asynchronous post back is to add an “end request” handler to the PageRequestManager.

In this way you can tell the request manager to run a javascript function on returning from a Asynchronous post back event of AJAX.

Code for doing this is :

function load()

{
   Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

where “EndRequestHandler” will be the name of your javascript function you want to call.
Call the above function in Onload event of <body> tag:

<body onload=”load()”>

function EndRequestHandler()

{

          alert(“You record has been saved successfully”);

}

Now If you want to give a different message based on your logic in server side code (code behind) then you can use a server side Hidden Field:

<input id=”hdnValue” type=”hidden” runat=”server”  value=”" />

Set its value in server side code on Asychronous Post Back: 

Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateSample.Click

    If condition Then

hdnValue.value = “do this”

    Else     

hdnValue.value = “do that”

    End If 

End Sub

Now you can check the value of this Hidden Field in your Client Side EndRequestHandler function and give a different alert to user based on its value:

function EndRequestHandler()
{
     if (document.getElementById(‘<%= hdnValue.ClientID %>’).value == “do this”)

     { 
          alert(“You record has been saved successfully”);
     } 
     else
     {
          alert(“There is an error”);
     }
 }

AddThis Social Bookmark Button  
View zeeshan malik's LinkedIn profileView zeeshan malik’s profile

25 Responses to “How to call client-side Javascript function after an UpdatePanel asychronous (Ajax) request is over”

  1. Chris said

    This functionality is exactly what I need, but does it require ASP.Net v3.5? I don’t see how to get the Sys.WebForms… when using v2.0. How would I accomplish the same outcome using v2.0? Thanks!

  2. Lukk said

    Hi,

    This post was exactly what I was looking for! :)

    Chris:
    it works with .net 2.0 – maybe you have to add some update panels and a script manager onto your page…

    Many thanks, zeemalik
    Lukk

  3. Spud said

    Awesome! The only decent article on the subject. Works in v.20.

    You can set any javascript to run on the server by doing :

    function EndRequestHandler()
    {
    eval(document.getElementById(’’).value);
    document.getElementById(’’).value = ”;
    }

    Works a beaut.

  4. Dan Cook said

    Life-saver. Cheers.

    As a small contribution, if you want to call something in the initial page load AND on any subsequent refresh of update panel, the codes below. Might save 5 mins of messing about.

    Then….

    function onBodyLoad()
    {
    //do something here
    load();
    }

    function EndRequestHandler(sender, args) {

    //do same thing here

    }

    function load() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    }

    • zeemalik said

      @Dan Cook
      Thanks for the input. But i didn’t get your point. What is the difference between your code in comments and the code i have written in my post?

      Also i want to accomplish the same task “if you want to call something in the initial page load AND on any subsequent refresh of update panel, the codes below.” using FaceBox (JQuery) but i am unable to do it yet. EndRequestHandler is not firing inside the FaceBox.
      Any idea how to accomplish this?

      Regards

      zeemalik78@yahoo.com

  5. Piyush Verma said

    This article is exactly same what I am looking for.
    Actually I am devloping a chat application and i wanted to change the title by calling a javascript function after the postback.

  6. Dan said

    THANKS! After adding scriptmanager and updatepanel I could not longer call my alert function. After adding your code, it works great! Thanks again for the time you spent to publish this solution.

  7. Naveed Mazhar said

    Nice article. Keep it up.

  8. [...] http://zeemalik.wordpress.com/2007/11/27/how-to-call-client-side-javascript-function-after-an-update... [...]

  9. Ola said

    Thanks, just what i needed .. me to ;)

  10. Nico said

    Very nice thanks.
    greez from Germany ;)

  11. shivakanth said

    Nice article
    Thanks

  12. Bruno said

    I have same problem, but I’am working with master pages, so I can’t put anything in tag because it’s not in content page. Have idea how to solve this?

    Thanks

  13. zeemalik said

    Hi Bruno! This is Zeemalik . Please explain your problem in detail so that i might be able to help you.

    http://www.linkedin.com/in/zeeshanmalik

    Regards

  14. Vidal said

    Hi Zeemalik,

    In my case I´m working with a jquery plugin, all the jquery functions are in a separated files (*.js) which I reference from my aspx file, in my aspx file I have an Update Panel which has inside a call to one of the functions in my jquery files that trigger all the functions that make the jquery component works, after an Update Panel asynchronous request the jquery component stop working… if I understand well your solution I should put all the code in my .js files within the EndRequestHandler(), is this right? if so, then I have to put a lot of code in there, or is there another way to do it? maybe I’m not getting your solution…

    Vidal

    • zeemalik said

      @Vidal. Thanks for your comments. You can try adding the code in .js files it might work. I am also facing similar kind of issue. I am using FaceBox (JQuery Plugin) and when i post back inside the FaceBox it do not fire EndRequestHandler. If you get lucky in making it work please also notify me with the solution.

      Regards,

      zeemalik78@yahoo.com

  15. Vidal said

    Thanks Zeemalik … I´m still working on it .. I´ll let you know if I make it work

  16. Vidal said

    Zeemalik, I´m trying right now with the ScriptManager.RegisterClientScriptInclude method, but I just can´t make it register my .js files, have you already tried this method before?

  17. BillE said

    This is a nice description of using a jQuery plugin “uploadify” in asp.net using the RegisterClientScriptInclude Methods. Works nice in an updatepanel.

  18. BillE said

    Looks like my link disappeared. Here again in plain text.

    http://www.uploadify.com/forum/viewtopic.php?f=7&t=142

  19. Neo said

    Thanks a lot!! this is exactly what i was looking for..

  20. Prakash said

    hi,

    how can i call my javascript method with parameters at the end of the request?

    for example,

    function DynamicPostBack(ControlId)
    {
    //method body
    }

  21. Nikhil said

    Hi zeemalik,

    My problem is I have a grid on page where we have updatepanel. On the grid there is Edit link which take to next page. On the next page I update the record and clicked save button. After updating the record, I click back button. On that Back button I have written Javascript History.go back. but when I click on this page it does not retain its state. i.e. If on the grid I was on 6 page, when I click back of Next page then I am on the first page.

    Update Panel and Javascript go back is not working in the same manner.

    If i remove update panel my problem is resolved the state is maintained on the grid but the data in the grid is not refreshed.

    Please help

  22. malappuram said

    this is what i exactly needed

  23. Heba said

    Many Thanks, thats reaaly helped, exactly what i was looking for :)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>