Tuesday, July 10, 2007

Paging Logic

I am trying to write a simple logic for paging the resultset of records.
It purely depends on a single querystring parameter like start = 161 or start=81
This would display records from 161 to 170 or 81 to 90.


int nStart = 1; //default start number i.e. very first record when first search hit.
int newStart;
string requestUri = Request.Url.AbsoluteUri; //actual complete URL
string newRequestUri;

if (Request.QueryString["start"] != null)
nStart = Convert.ToInt16(Request.QueryString["start"]);

int nMaxResultsInAPage = 10;
int pageNumber; //current page number

if (nStart == 1) //initially not divisible by 10 as it is possible with page 2nd and record 11
pageNumber = 1;
else //current page number....as start gives the record number
pageNumber = nStart / 10; // 2 = 21 / 10



//j... ... ...refers to the paging number to be iterated
//count... ... ...how many paging numbers to be shown in total for navigation

for (int j = pageNumber - 10 / 2, count = 0;count <> 0)
{
newStart = (j-1) * 10 + 1; //4th page represents records 31 to 40...so [4th Page] = 3 * 10 + 1
newRequestUri = requestUri.Replace("start=" + nStart.ToString(), "start=" + Convert.ToString(newStart));
if(count == 0)
ltrPageLinks.Text += " Previous - ";
else if (count == 11)
ltrPageLinks.Text += " Next - ";
else if (j == nStart / 10 - 1)
ltrPageLinks.Text += "" + j.ToString() + " - ";
else
ltrPageLinks.Text += "" + j.ToString() + " - ";


}
}

No comments:

 
Dotster Domain Registration Special Offer