Using a SPGridView inside an ASP.net Ajax UpdatePanel

The goal of this post is to demonstrate how to use ASP.net Ajax Extensions inside of WSS to build web parts and controls that are capable of doing async post backs and partial page refreshes. This post will also cover how to use the SPGridView with paging and sorting.

How it works

The web part in the example code uses an SPGridView control to display a data table.  The grid is inside a UpdatePanel which allows all the sorting and pagination to happen asynchronously. What’s all this mean? It means that you can sort and page your grid view without having to refresh the rest of page. This is great for SharePoint because pages can typically have a long life cycle. One thing that is really cool about this approach is that it doesn’t require you to write any javascript at all. The UpdatePanel takes care of all that stuff behind the scenes. You can take the code used in this example and apply it to any other application the UpdatePanel can be used for, such as data entry. You can also use the ASP.net Ajax Control Extenders.

The real secret sauce here is the AjaxBaseWebpart class (credit: Eric). If you aren’t aware already, SharePoint doesn’t play nice with the ASP.net Ajax framework out of the box, so we have to do some ninja tricks to get it to work. This class makes sure that the page contains a ScriptManager which is required by the ASP.net Ajax framework to do partial page refreshes and other ajaxy stuff. It also tweaks some of the SharePoint related JavaScript to allow the UpdatePanel and other Ajax controls to handle the post back correctly. If you want the details on the AjaxBaseWebpart look at Erics post.

Get the sample running..

  1. Modify your SharePoint web.config file with the appropriate settings for the ASP.net Ajax Extensions. Mike Ammerlaan has the details on this.
  2. Download the sample code, build, and install. I have a install.bat file in the bin\debug dir of the cab project that will help you install the web part, just change the server name before you run it. (might have to remove –force the first time)

Know bugs/Problems

  • For some weird reason the first time you use the pagination or sorting it does a refresh of the entire page,  but every time after that it works correctly. Kind of a tweaky little bug, if any one figures out why this is happening please let me know. The updatepanel only seems to do this with the SPGridView.
  • Deployment with all the web.cofig changes is not going to be fun. You will probably need to build a solution package that makes the modifications for you are part of the install process.

Credits/Sources

This is example project is a combination of code and information from several people. Thanks to

 Let me know if you have any questions or improvements.

- Mark Collins

kick it on SharePointKicks.com

posted @ Wednesday, February 28, 2007 4:53 PM

Print