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   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<September 2008>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Comment Stats

DayTotal% of Total
Sunday 1926.8%
Monday 39714.1%
Tuesday 47316.8%
Wednesday 52318.5%
Thursday 54619.4%
Friday 51418.2%
Saturday 1756.2%
Total 2820100.0%

Hour1Total% of Total
12:00 AM 692.4%
1:00 AM 712.5%
2:00 AM 642.3%
3:00 AM 762.7%
4:00 AM 592.1%
5:00 AM 1124.0%
6:00 AM 1113.9%
7:00 AM 1625.7%
8:00 AM 1776.3%
9:00 AM 1515.4%
10:00 AM 1756.2%
11:00 AM 1836.5%
12:00 PM 1926.8%
1:00 PM 1786.3%
2:00 PM 1615.7%
3:00 PM 1344.8%
4:00 PM 1134.0%
5:00 PM 993.5%
6:00 PM 933.3%
7:00 PM 993.5%
8:00 PM 883.1%
9:00 PM 812.9%
10:00 PM 832.9%
11:00 PM 893.2%
Total 2820100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.28359
Tuesday 4.31448
Wednesday 7.52647
Thursday 6.82634
Friday 5.32420
Saturday 5.09168
Total 5.742820

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.29111
9:00 AM 6.09280
10:00 AM 6.27257
11:00 AM 4.28184
12:00 PM 6.86343
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.57300
4:00 PM 3.7694
5:00 PM 5.79162
6:00 PM 4.56114
7:00 PM 9.58182
8:00 PM 8.78158
9:00 PM 5.18114
10:00 PM 5.9383
11:00 PM 4.5732
Total 5.742820

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles