I got it working a while back. (I actually needed to use nested
repeaters,
but I omitted the 2nd one in the code below.)
I treat it just like a grid. The key is to use the PagedDataSource class.
Here is some sample code - hope it makes some sense.
============================================================
Page level variables:
Protected pagedData As New PagedDataSource
Private mPageNo As Integer = 1
Private strPages As String
Private mSortDirection As ListSortDirection =
ListSortDirection.Ascending
Private mSortExpression As String = "invnumber"
Private mControlToSort As String = "lnkInvnumber"
============================================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'do stuff here
doDataBind()
End Sub
============================================================
Private Sub doDataBind()
Me.lblTotal.Text = "Total Records: " & CStr(myCollection.Count)
myCollection.Sort(mSortExpression, mSortDirection,
StableSortSetting.Yes)
If Not Request.QueryString("PageNo") Is Nothing Then
If IsNumeric(Request.QueryString("PageNo")) Then
mPageNo = CInt(Request.QueryString("PageNo"))
End If
End If
pagedData.CurrentPageIndex = mPageNo - 1
pagedData.DataSource = myCollection
pagedData.AllowPaging = True
pagedData.PageSize = 20
strPages = DrawPaging(mPageNo, pagedData.PageCount)
rptrInvHdr.DataSource = pagedData
DataBind()
End Sub
============================================================
Protected Sub doSort(ByVal source As Object, ByVal e As
RepeaterCommandEventArgs)
If e.Item.ItemType = ListItemType.Header Then
'Note: the name of each LinkButton in the Repeater header needs to
have a 3 letter prefix
'like lnk or btn and the rest of the name needs to match the BO
property.
Dim mBtn As LinkButton = CType(e.CommandSource, LinkButton)
mControlToSort = mBtn.ID
'strip off the 3 letter prefix. e.g lnkInvnumber becomes Invnumber
Dim strSortExpression As String = mBtn.ID.Substring(3)
If String.Compare(strSortExpression, mSortExpression, True) = 0
Then
If mSortDirection = ListSortDirection.Ascending Then
mSortDirection = ListSortDirection.Descending
Else
mSortDirection = ListSortDirection.Ascending
End If
Else
'user clicked a different header so start the sort as ASC
mSortDirection = ListSortDirection.Ascending
End If
Viewstate("sortDirection") = mSortDirection
'update the sort value AFTER the comparison between new and old
values
has been made.
mSortExpression = strSortExpression
Viewstate("sortExpression") = mSortExpression
'go to the first page when a sort is performed
Viewstate("pageIndex") = 0
doDataBind()
End If
End Sub
Private Sub rptrInvHdr_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
rptrInvHdr.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim myItem As SomeItem = CType(e.Item.DataItem, myItem)
Dim oLblHdrDescr As Label =
CType(e.Item.FindControl("lblHdrDescr"),
Label)
oLblHdrDescr.Text = "<b>" & myItem.descr & "</b>"
Dim oHylPrev As HyperLink = CType(e.Item.FindControl("hylPrev"),
HyperLink)
oHylPrev.NavigateUrl = "..."
End If
If e.Item.ItemType = ListItemType.Footer Then
Dim oLblPages As Label = CType(e.Item.FindControl("lblPages"),
Label)
oLblPages.Text = strPages
End If
End Sub
==============================================================
'<summary>
'Returns an HTML string of page numbers as hyperlinks. Includes
Previous
10, Previous, Next and Next 10 links.
'</summary>
'<param name="pageNumber">Current Page number - must be greater than
0
</param>
'<param name="pageCount">Total number of pages</param>
'<returns>string</returns>
Public Shared Function DrawPaging(ByVal pageNumber As Integer, ByVal
pageCount As Integer) As String
Dim sb As New StringBuilder
Dim x, y, z, pageEnd, pageStart As Integer
If pageCount > 1 Then
'handle 10 at a time
'get the start and end
If pageNumber Mod 10 = 0 Then '10, 20, 30 appear with old set
pageStart = pageNumber - 9
Else
y = pageNumber.ToString.Length
If y = 1 Then '1 - 9
pageStart = 1
Else
z = CInt(Left(pageNumber.ToString, y - 1))
pageStart = (z * 10) + 1
End If
End If
If pageStart + 9 > pageCount Then
pageEnd = pageCount
Else
pageEnd = pageStart + 9
End If
'draw the Previous 10 if not in the first 10
If pageStart > 10 Then
sb.Append("<a href=")
sb.Append(DrawLink(pageStart - 1))
sb.Append(">Previous 10</a> ")
End If
'draw the previous button if not at the first item on the first
page
If pageNumber <> 1 Then
sb.Append("<a href=")
sb.Append(DrawLink(pageNumber - 1))
sb.Append("><</a> ")
Else
sb.Append(" ")
End If
'draw the page numbers (current page is not a hyperlink it is in
bold in square brackets)
For x = pageStart To pageEnd
If x = pageNumber Then
sb.Append("<strong>[")
sb.Append(x)
sb.Append("]</strong> ")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a> ")
End If
Next
'draw the next button if not at the last page
If pageNumber < pageCount Then
sb.Append("<a href=")
sb.Append(DrawLink(pageNumber + 1))
sb.Append(">></a>")
Else
sb.Append(" ")
End If
'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append(" <a href=")
sb.Append(DrawLink(pageEnd + 1))
sb.Append(">Next 10</a>")
End If
sb.Append("<small><br>Total Page Count: ")
sb.Append(pageCount)
sb.Append("</small>")
sb.Append("</CENTER>")
Return sb.ToString
End If
End Function
==============================================================
Private Shared Function DrawLink(ByVal pageNumber As Integer) As
String
Return "?PageNo=" & pageNumber
End Function
==============================================================
--
Joe Fallon
Matthew Curiale said:
I am creating an app that lists clients of a company for management of
different attributes for that company. The first page is a listing of
the companies currently in the database. I have my repeater working,
and paging/sorting works, but there is a small bug that I can't seem
to figure out.
If, for example, I display 5 records per page, the paging function
executes fine, and only shows 5 records per page. This is no problem.
When I sort the list, though, on any of the pages, it will show the
OTHER 5 records... Something along these lines:
Page 1 -> Page 1 (sorted)
------ ---------------
Comp1 Comp10
Comp2 Comp9
Comp3 Comp8
Comp4 Comp7
Comp5 Comp6
I need it to work so that on the sort for the first page, the first 5
records are the only ones that are sorted. How can I work around this?
It happens on the second page, also. After sorting, the first 5
records are shown, and not the last 5 for that page.
I know I can do this using a datagrid no problem, but I would really
like it to work with a repeater. I'm using a dataset to hold the
records through a stored procedure. Any ideas?
I'm not sure what code I should post, so please let me know if that is
needed.
Matt
ps: Please CC me with the reply.