Asynchronous GridView in 5 simple steps

Mar 9, 2007

A web page with a data bound GridView control can take a long time to load. The page is not rendered until all the controls are, and the GridView cannot render before data has been retrieved from the database. So, let’s load the GridView asynchronous and make the page load faster and the perceived speed greater.

This little trick is actually very simple and it involves the built-in client callback feature of ASP.NET. Even though I’m not a big fan of that particular feature, it can be quite handy from time to time. The best part of this is that you don’t have to change much to your existing code.

Web page

On the webpage that hosts the GridView control, you have to make 2 small changes.

Step 1. Encapsulate the GridView
The GridView control has to be encapsulated by an HTML control with an id attribute, so that it can be referenced by JavaScript. That is because the GridView does not render when no data is bound to it.

<div id="grid">
 <span>Loading...</span>
 <asp:GridView runat="Server" ID="gvAsync" />
</div>

Step 2. Add a JavaScript
Then a JavaScript is needed to load the rendered and data bound GridView to the HTML control we added in step 1. Place the JavaScript below the GridView’s surrounding div tag.

<script type="text/javascript">
function EndGetData(arg)
{
 document.getElementById("grid").innerHTML = arg;
}

setTimeout("<asp:literal runat="server" id="ltCallback" />", 100);
</script>

Code-behind

In the code-behind file there are three small steps to perform.

Step 3. Implement ICallbackEventHandler
We need to implement the interface to turn on the client callback feature.

public partial class asyncgridview : System.Web.UI.Page, ICallbackEventHandler

Step 4. Bind the call back reference
The literal control placed in the JavaScript function in step 2 has to contain the client callback reference. Add the following to the Page_Load.

if (!Page.IsCallback)
 ltCallback.Text = ClientScript.GetCallbackEventReference(this, "'bindgrid'", "EndGetData", "'asyncgrid'", false);

Step 5. Bind the grid and return the rendered HTML
To finish the asynchronous loading we have to implement the two methods that are defined by the ICallbackEventHandler interface we implemented in step 3. One of the methods binds a DataTable to the GridView and renders the control. The second returns the rendered HTML to the JavaScript method we defined in step 2.

private string _Callback;

public string GetCallbackResult()
{
 return _Callback;
}

public void RaiseCallbackEvent(string eventArgument)
{
 DataTable table = RetrieveDataTableFromDatabase();
 gvAsync.DataSource = table;
 gvAsync.DataBind();

 using (System.IO.StringWriter sw = new System.IO.StringWriter())
 {
  gvAsync.RenderControl(new HtmlTextWriter(sw));
  _Callback = sw.ToString();
 }
}

You can use your present data binding method to bind the GridView. The important part is that the GridView is data bound before the RaiseCallbackEvent method renders the control.

The same technique can be used for all the data control such as the Repeater, FormView and DataList.

Download

asyncgridview.zip (1,21 KB)

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (10) -

Brad
Brad
3/10/2007 2:37:37 AM #

Honest question... does putting a gridview in an ASP.NET AJAX UpdatePanel achieve the same effect?

Mads Kristensen
Mads Kristensen
3/10/2007 8:50:14 AM #

I honestly don't know Brad. I don't use ASP.NET AJAX. My guess is no, because the UpdatePanel is not aware of the databinding method in the code-behind.

 Eric D&gt; Burdo
Eric D> Burdo
3/12/2007 9:50:24 AM #

One caveat - This doesn't work with Custom Paging and Sorting using an objectdatasource.

I am sure I could probably make it work, but I don't have the time to try it.  

Mads Kristensen
Mads Kristensen
3/12/2007 10:10:43 AM #

Probably not. I have it working with custom paging and sorting without the object datasource.

Matt Hawley
Matt Hawley
3/12/2007 12:06:36 PM #

Surrounding a GridView in an UpdatePanel does achieve the same effect, and much simplier I might add. No additional code is needed.

Mads Kristensen
Mads Kristensen
3/12/2007 12:12:05 PM #

That's fine and all, but not everybody is using ASP.NET AJAX. Personally I'm not a fan.

 Manu Temmerman-Uyttenbroeck
Manu Temmerman-Uyttenbroeck
3/20/2007 5:03:25 AM #

With Anthem it would've been sufficient to change &lt;asp:GridView&gt; ... &lt;/asp:GridView&gt; into &lt;anthem:GridView&gt; ... &lt;/anthem:GridView&gt;.
As already mentioned before; you should really really have a look at it Mads Smile May I ask why you're not a fan of ASP.NET AJAX?

Mads Kristensen
Mads Kristensen
3/20/2007 6:13:34 AM #

That's smart with Anthem, but it is probably sort of the same that happens when you use ASP.NET AJAX and surrounds the GridView with an UpdatePanel. I don't like ASP.NET AJAX because it takes to many decisions on my behalf. I want to be in control of the output at all times without exception. And then again, I haven't played around with it much, so it is not a qualified answer.

 Michel Metselaar
Michel Metselaar
4/1/2007 8:23:08 AM #

Dear Mads,

When I add an event handler for OnSelectedIndexChanged it doesn't fire. Do you have a sugestion? Thanks in advance!

Regards,
Michel

sara
sara Iran
10/23/2007 11:05:50 AM #

thanks a lot!

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.