Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

JavaScript function to be called after UpdatePanel refreshes

4.00/5 (1 vote)
16 Jul 2011CPOL 15K  
A method to call a function in JavaScript after each response.

I was searching for a method to call a function in JavaScript after each response. I found a method to do this:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded


I also made a function to manage the loading of DOM objects.
JavaScript
$.PlanetAspNet.setReady('.hint',function(hint){
    hint.each(function(){
        var $span = $(this);
        $span.siblings('input')
             .focus(function(){ $span.show()})
             .blur(function(){ $span.hide()});

Here is the JavaScript function after UpdatePanel refreshes. In this case, the function will be called on document ready and after each DOM object is created after ASPNET Async.


JavaScript
$.PlanetAspNet = {
    options: [],
    PageLoadedHandler: function(sender,e){
        if (e._panelsUpdated.length==0){
            $.PlanetAspNet.Ready(document,e);
        }else{
            $.each(e._panelsUpdated,function(){
                $.PlanetAspNet.Ready(this,e);
            });
        };
    },
    setReady: function(selector,fn){
        $.PlanetAspNet.options.push({selector:selector,fn:fn});
        return $.PlanetAspNet.options;
    },
    Ready: function(item,e){
        $.each($.PlanetAspNet.options,function(){
            var o = $(item).find(this.selector);
            if (o.length>
0) this.fn(o,item,e);
        });
        return item;
    }
};

I hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)