By David Reed
Alvin had a good start in his
article, but the recommendations in it are NOT ideal from a usability
perspective, IMNSHO. However, his basic suggestions are better than errors in
production. Heh.
This way the user gets the functionality of stepping backward through the pages
instead of having to start all over on Page 01 every time the page count shifts
unexpectedly.
Because the HttpException with the message "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount." usually happens when the user is allowed to delete records from the
results set on the fly (or in high concurrency apps where other users can
affect the results set underneath you), what is ideal is the following
refactor:
-
Create a single method for popping the grid (ex, PopGrid(...) ) in all cases.
Always a good idea anyway.
-
Inside PopGrid(), recurse when this specific error occurs:
private void PopGrid( ... )
{
try
{
dgList.DataBind();
}
catch( System.Web.HttpException ex )
{
if( ex.Message == "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount." _
&& dgList.CurrentPageIndex > 0 )
{
dgList.CurrentPageIndex--;
PopGrid();
}
else
{
throw;
}
}
}
-
This method will just backpedal through the pages as the user deletes items
from the grid.
-
The nasty error will only bubble when the CurrentPageIndex is set below 0
intentionally.
Strong Warning: If you have a potentially LARGE number of pages
(more than 20 or so) or a high concurrency app (or both), I would strongly
consider NOT using this method.