DataGrid paging with ViewState off

ilya2

Freshman
Joined
Apr 13, 2004
Messages
29
Location
Slovakia
Hi,

I wanted to create a pageable DataGrid. Everything was OK, until I switched ViewState off. The problem is that when I click at last page and then I click at some else, everything returns to the first page. When I have a "left-right" paging, I can't get through the second page to the third.

Has anybody met this problem before?
Thanks
 
ilya2 said:
Hi,

I wanted to create a pageable DataGrid. Everything was OK, until I switched ViewState off. The problem is that when I click at last page and then I click at some else, everything returns to the first page. When I have a "left-right" paging, I can't get through the second page to the third.

Has anybody met this problem before?
Thanks

Is there a reason you turned off the ViewState? That's what remember's the page that you're viewing. Otherwise, you're going to have to write a lot of code to store the page you're on and get that value when you come back to it.
 
Mothra said:
Is there a reason you turned off the ViewState? That's what remember's the page that you're viewing. Otherwise, you're going to have to write a lot of code to store the page you're on and get that value when you come back to it.

Yes, the reason is that I don't want to store large amount of data in a page. I rather want to get the data from DB every time.

I know there is another option for me, to use custom paging, but it seems to be rather complicated to filter or return only the rows for current page.

I just don't understand, why the page number doesn't switch correctly when I bind the grid with a new datasource at page load and set the pageindex at corresponding event. However, as I mentioned before, it DOES switch correctly until I try to switch back from the last page to another.
 
The solution is...

I found out that this could be a possible reason of this behaviour:

The paging controls in a grid have their IDs which are used to identify which event is to be raised by the postback. However, these IDs depend on how many rows are there currently shown in the grid (when paging controls are at bottom).

So, when I turn the ViewState off, after postback there are no rows in grid, because they are not reconstructed. The grid now doesn't know how to resolve the IDs, it doesn't know it belongs to a page number a it doesn't raise PageIndexChanged event.

I've solved this problem by binding the grid to some default data. Actually I've had to remember the last number of rows shown in the grid and store it to Session. By the postback, I fill the grid with default data so that it has the same amount of rows. I also have to set VirtualItemCount (I use custom paging) to some big number and CurrentPageIndex to the last selected page, which I also have stored. If I wouldn't do that and leave the CurrentPageIndex at 0, selecting the first page wouldn't lead to raising PageIndexChanged event again.

Maybe this isn't quite a clear solution, however, it works now ;)
 
Back
Top