This function, as the name suggests, creates the pager for
our HTML Grid. It accepts the following Parameters:
intRowCount – Total No of Records in the DataView
intPageNo – Current Page Index
intRowsPerPage – Number of Records Per Page
intPageSet – Index of the Current Page
Here you will notice that I have used a word called PageSet.
It is a set of Pages in the pager .
Figure 1
The above figure refers to a PageSet with index 1. It is
showing 10 Pages since I have set the intPagesPerSet to 10.
First, I am calculating the total number for pages for the
current set of data. This is done by dividing the Total Number of Records with
the Number of records per Page
Listing 9
dblPageCount = Convert.ToDecimal(intRowCount) / Convert.ToDecimal(intRowsPerPage)
intPageCount = Convert.ToInt32(Math.Ceiling(dblPageCount))
Then I am calculating the start page index and the end page
index of the PageSet based on the intPageSet and the intPagesPerSet. Note that
I have set the Pages per Set to 10. This means that on the Grid I will see Page
Numbers 1 to 10, in the next set 11 to 20 and so on.
Listing 10
intPagesPerSet = 10
intStartPage = ((intPageSet - 1) * intPagesPerSet) + 1
intEndPage = intPageSet * intPagesPerSet
As you can see, I am creating an HTML markup here too, using
a String builder which we will be binding to the dvPager. See the complete
function below.
Listing 11
Private Function Pager(ByVal intRowCount As Integer, ByVal intPageNo As Integer, _
ByVal intRowsPerPage As Integer, ByVal intPageSet As Integer) As String
Dim strResults As New StringBuilder
Dim intPageCount As Integer
Dim dblPageCount As Double
dblPageCount=Convert.ToDecimal(intRowCount)/Convert.ToDecimal(intRowsPerPage)
intPageCount = Convert.ToInt32(Math.Ceiling(dblPageCount))
If intPageNo > intPageCount Or intPageNo < 1 Then
intPageNo = 1
End If
Dim intStartPage, intEndPage, intPagesPerSet As Integer
intPagesPerSet = 10
intStartPage = ((intPageSet - 1) * intPagesPerSet) + 1
intEndPage = intPageSet * intPagesPerSet
If intEndPage > intPageCount Then
intEndPage = intPageCount
End If
If intPageSet > 1 Then
strResults.Append( _
"<a href = 'javascript:;' onclick = 'PrepareRequest(")
strResults.Append(intStartPage - intPagesPerSet)
strResults.Append(", ")
strResults.Append(intPageSet - 1)
strResults.Append(")' class='pager'><<<</a> ")
End If
For k As Integer = intStartPage To intEndPage
If k = intPageNo Then
strResults.Append("<span class='pager'>")
strResults.Append(k)
strResults.Append("</span>")
Else
strResults.Append( _
"<a href = 'javascript:;' onclick = 'PrepareRequest(")
strResults.Append(k)
strResults.Append(", ")
strResults.Append(intPageSet)
strResults.Append(")' class='pager'>")
strResults.Append(k)
strResults.Append("</a>")
Dim str As String = strResults.ToString
End If
strResults.Append(" ")
Next
If intPageCount > intEndPage Then
strResults.Append( _
"<a href = 'javascript:;' onclick = 'PrepareRequest(")
strResults.Append(intEndPage + 1)
strResults.Append(", ")
strResults.Append(intPageSet + 1)
strResults.Append(")' class='pager'>>>></a>")
End If
Return strResults.ToString()
End Function