2.15. Updating a GridView Without Refreshing the Whole Page

Problem

You want to use a GridView and have its contents be updated without refreshing the whole page when the user performs a sort or paging operation.

Solution

Implement the solution described in Recipe 2.14 and then set the EnableSortingAndPagingCallbacks attribute of the GridView to true.

Discussion

The GridView control provides built-in support for making callbacks to the server when a new sort order or page of data is requested by the user, and, in the process, retrieves the most recent data and updates the contents of the GridView, all without having to refresh the whole page. All you have to do to implement this capability is to set the EnableSortingAndPagingCallbacks attribute of the GridView to true. ASP.NET handles everything else for you.

	<asp:GridView ID="gvBooks" Runat="Server"
					AllowPaging="true"
					AllowSorting="true"
					AutoGenerateColumns="false"
					BorderColor="#000080"
					BorderStyle="Solid"
					BorderWidth="2px"
					Caption=""
					HorizontalAlign="Center"
					Width="90%"
					PageSize="5"
					PagerSettings-Mode="Numeric"
					PagerSettings-PageButtonCount="5"
					PagerSettings-Position="Bottom"
					PagerStyle-HorizontalAlign="Center"
					PagerSettings-NextPageText="Next"
					PagerSettings-PreviousPageText="Prev"
					PagerStyle-CssClass="pagerText"
					OnRowCreated="gvBooks_RowCreated"
					EnableSortingAndPagingCallbacks="true" >

ASP.NET handles the partial page refresh for you by outputting client-side JavaScript to perform an asynchronous callback to the server and handle ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.