Denny.NET

I can haz ASP.NET goodness?

About the author

Denny Ferrassoli
Developer at Casting Networks. MCP / .NET
E-mail me Send mail
Add to Technorati Favorites

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Animating ASP.NET AJAX Update Panel Updates

Do you dislike the way that ASP.NET AJAX Update Panels update (or replace the content within the update panel)? Is it not flashy and Web 2.0 enough for you? Well fear no more! I've devised a straight-forward script to help you animate your update panel updates. This script uses jQuery (www.jquery.com) to animate the content when an update panel updates. It can easily be modified to use another library such as Scriptaculous. It uses javaScript to override ASP.NET AJAX's _updatePanel method within the PageRequestManager (the method which does the actual replacing of the content).

I've commented the script so you can understand what it's doing:

[code:js]
$(document).ready(
  function() {
      // SAVE the original _updatePanel function for later use.
      var old_prm_updatePanel = Sys.WebForms.PageRequestManager.getInstance()._updatePanel; 

      // Overwrite the original method with our code which will apply animation.
      // (Note: The method signature remains the same)
      Sys.WebForms.PageRequestManager.getInstance()._updatePanel = 
          function(updatePanelElement, rendering) {
              // Using jQuery to hide (animate) the element. Then use the callback function 
              // to load and show the new data. You don't want to update the
              // data beforehand otherwise you'll see your update panel flicker.
              $(updatePanelElement).hide('slow',
                  function() {
                      // Pass the old function and the args
                      loadShowUpdatePanel(old_prm_updatePanel, updatePanelElement, rendering);
                  });
          }
   });
 
function loadShowUpdatePanel(old_prm_updatePanel, updatePanelElement, rendering) {
    // Call the original (old) function with context of Page Request Manager instance.
    // We call the original method so ASP.NET AJAX can do all the necessary actions.
    old_prm_updatePanel.apply(Sys.WebForms.PageRequestManager.getInstance(),
                                                               new Array(updatePanelElement, rendering));

    // using jQuery again to show (animate) the update panel.
    $(updatePanelElement).show('slow');
}
[/code]

And that's all you need. This script isn't just limited to animating the update panel update either... You can extend this script to do anything you want immediately before and after the new content is displayed. The main difference between this method and using the add_BeginRequest or add_EndRequest methods is the timing. This script will fire exactly before and after the update takes place - This would not be a good place to show a loading screen because the AJAX call has already been made, there's nothing to wait for. Whereas the BeginRequest executes before the AJAX call and EndRequest fires after the update has completed - This would be good to apply a loading screen.

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: ASP.NET | Client-Side
Posted by Denny on Thursday, November 08, 2007 11:08 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Comments

DotNetKicks.com

Friday, November 09, 2007 7:50 AM

trackback

Trackback from DotNetKicks.com

Animating ASP.NET AJAX Update Panel Updates

Kevin Jensen us

Tuesday, November 13, 2007 1:48 PM

Kevin Jensen

Denny,

Cool article. Do you have a live demo of this avail?

Kevin

David Kemp gb

Wednesday, November 14, 2007 5:46 AM

David Kemp

from asp.net/.../default.aspx

"note

This class [PageRequestManager] contains private members that support the client-script infrastructure and are not intended to be used directly from your code. Names of private members begin with an underscore ( _ )."

I'd suggest, based on this, that your code /might/ break when there's an update to ASP.NET AJAX.

It's neat code despite that though Laughing

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading