Scott on Writing

Musings on technical writing...

Custom Paging and Navigating to the Last Page

My latest set of Working with Data in ASP.NET 2.0 tutorials focused on paging and sorting data and included a look at implementing custom paging (with sorting) with the GridView and ObjectDataSource. Alert reader Mark Fox pointed out that there's a bit of a problem when using custom paging and clicking the Last page link in the paging interface:

For lesson 25 – Efficient Paging I downloaded the file ASPNET_Data_Tutorial_25_VB.exe and than opened the project in VS 2005 Standard and ran PagingAndSorting/EfficientPaging.aspx. Clicking the PageLast >> button in GridView1’s pager gives a System.OverflowException in System.Web.UI.WebControls.GridView.CreateDataSourceSelectArguments().

Enabling the viewstate in GridView1 fixes this.

With custom paging, the page count value returned by the ObjectDataSource’s SelectCountMethod is stored in the GridView’s view state. Other GridView variables – the PageIndex, EditIndex, SelectedIndex, DataKeys collection, and so on – are stored in control state, which is persisted regardless of the value of the GridView’s EnableViewState property. Since the PageCount value is persisted across postbacks using view state, when using a paging interface that includes a link to take you to the last page, it is imperative that the GridView’s view state be enabled. (If your paging interface does not include a direct link to the last page, then you may disable view state.)

Clicking the last page link causes a postback and instructs the GridView to update its PageIndex property. If the last page link is clicked, the GridView assigns its PageIndex property to a value one less than its PageCount property. With view state disabled, the PageCount value is lost across postbacks and the PageIndex is assigned the maximum integer value instead. Next, the GridView attempts to determine the starting row index by multiplying the PageSize and PageCount properties. This results in an OverflowException since the product exceeds the maximum allowed integer size.

To see the problem in code, use Reflector and drill into the GridView's HandleEvent method. There you'll find a switch statement that handles the different types of events, one of them being when the last page link is clicked:

if (base.IsViewStateEnabled)
{
  num1 = this.PageCount - 1;
}
else
{
  num1 = 0x7fffffff;
}

So if view state has been disabled, then num1 (the page the GridView is going to try to access) is set to the maximum integer value (2.147 billion, roughly). Next, look in the GridView's CreateDataSourceSelectArguments() method, and there you'll find:

arguments1.StartRowIndex = this.PageSize * this.PageIndex;

That multiplication right there is what causes the OverflowException, as any PageSize value greater than 1 will result in a value that exceeds the largest possible integer value.

The fix? Enable view state in your GridView so that the PageCount property can be remembered across postbacks.

posted on Friday, August 18, 2006 9:46 AM

Feedback

# Custom Pagind and Navigating to the Last Page 8/18/2006 11:06 AM while(availableTime>0) {

Scott Mitchell just published another great set of Working with Data in ASP.NET 2.0 tutorials. It´s...

# re: Custom Paging and Navigating to the Last Page 8/18/2006 12:59 PM O.Omari

What about overriding GriView's PageCount virtual property getter and saving ViewState["PageCount"] to control state at last stage before ViewState and control state stored?

# re: Custom Paging and Navigating to the Last Page 8/18/2006 1:16 PM Scott Mitchell

Omari, you could override the PageCount property, but you couldn't write it to the GridView's control state because the SaveControlState() method is internal.

However, you could override the property and serialize/deserialize it yourself, like to some hidden field on the page, I imagine. Of course, just enabling view state would be easier, but if the bloat introduced by view state was too big of an issue, this other approach might cut the mustard... or just don't have a Last page link in your paging interface... or maybe you could update the PageCount property in the PageIndexChanging event handler... there may be many solutions, but the observation I wanted to point out was that disabling view state + custom paging + clicking the Last page link = bad vodoo.

# re: Custom Paging and Navigating to the Last Page 8/27/2006 2:07 AM James Zhao

Hi,

In your lesson 27, I tried to add the page function. When I sorted a column, something strange happened to the bottom row of the grid where it was the page indicator which was all put into the first column rather than spaned all the columns as usual. Can you please take a look at the case? Thanks.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles