views:

186

answers:

2

Without paging function, i can loop through the gridview by using

var sum = 0;
var gridViewCtlId = '<%=timesheetView.ClientID%>';
var grid = document.getElementById(gridViewCtlId);
var gridLength = grid.rows.length;

so with gridLength i can loop through the gridview to sum all rows. However, when I use paging event of gridview, i use the page size to loop through all rows, but it occurs errors because the last page may not have enough rows. So would you please to help me how to get the rows in the each page of gridview?

A: 

What you have is correct, just update the gridLength property just before calculating the sum. There's no way for javascript to know the number of rows until the grid is there and present on the page...but that's what you're doing already, so just update that row count.

The only way I picture what you currently have not working is you have this code running initially and not when an UpdatePanel (or other loading mechanism) comes back, just re-grabbing the gridLength will resolve this, as the updated <table> in the DOM will have the correct number of rows.

Nick Craver
the problem is i need to loop through all the rows to calculate to total but the gridLength returns the totals of rows in the gridviewFor example: page size of grid view is 3, the total of records in database is 4. so if i loop from 0 to gridLength it will out of bound because the gridLength is 4. if i set the loop to page size (3), when it moves to page 2 it will occurs out of bound again 'cause in this page it only has 1 rows left.
louis
A: 

In your loop, you can do a check like this

if (grid.rows[rowIndex]){
// Your code to calculate sum goes here
}
RaviG