By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,719 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,719 IT Pros & Developers. It's quick & easy.

Sorting and Paging in a repeater

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Well, you have to change the offset of the range of records you are
currently viewing. Lets take your example, and assume there is a total
of 10 records. You were first viewing the first 5 (range = 1-5). After
changing the sort order, you end up viewing the first 5 records in the
desc. sorted resultset, which is 6-10.

What you could do is view the records from (size_of_resultset -
pagenumber * pagesize + 1) to (size_of_resultset - (pagenumber - 1) *
pagesize). In your example, that would be the records (10 - 1 * 5 + 1)
- (10 - 0 * 5) = 6 - 10.

Then again, have you looked at the datalist aswell?

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

Nov 19 '05 #2

P: n/a
I don't think it is a bug in your code. The sorting is supposed to work this
way only. It sorts descending or ascending for the entire recordset and not
just for the the rows displayed. also i think same behaviour should be seen
in datagrid also.
"Matthew Curiale" <ma*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
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.

Nov 19 '05 #3

P: n/a
I'm actually so close to finishing the project, that I just need this
to work, and it should be alright. I don't want to implement another
webcontrol right now; that's something that I can look into when the
project is over.

Where exactly would I insert that equation? Here is my code for the
paging and sorting.

Sub doPaging()
pagedData.DataSource = queryClients().DefaultView
pagedData.AllowPaging = True
pagedData.PageSize = 10
Try
pagedData.CurrentPageIndex =
Int32.Parse(Request.QueryString("Page")).ToString( )
Catch ex As Exception
pagedData.CurrentPageIndex = 0
End Try

'btnPrev.Visible = (Not pagedData.IsFirstPage)
'btnNext.Visible = (Not pagedData.IsLastPage)
page_num.value = pagedData.CurrentPageIndex
rpClientList.DataSource = pagedData
rpClientList.DataBind()
If Not company_id.Value = "" Then
Session("company_name") = Request.Form("client_name")
Session("tempID") = Request.Form("company_id")
Response.Redirect("NextPage.aspx")
End If
End Sub
Function queryClients() As DataTable
Dim CE_DSN As String = "connectionString"
Dim connection As New SqlConnection(CE_DSN)
Dim cmdClients As New SqlCommand("sproc", connection)
Dim prmOrderClient As New SqlParameter
Dim dsclients As New DataSet
Dim sql_search As String

connection.Open()
'if there is something in the search textbox, then populate the
sql_search variable
'so that it can be sent to the stored procedure. If not, its
default value is nothing.
If Len(txtClient.Text) > 0 Then
sql_search = "( comp.companyName like '%" & txtClient.Text
& "%' or domain_id.domain like '%" & txtClient.Text & "%' )"
Else
sql_search = ""
End If

With prmOrderClient
.ParameterName = "@order_by"
.SqlDbType = SqlDbType.NVarChar
'if the hidden fields on the form are empty, then set the
values of this parameter, and the
'two hidden fields to defaults
If Request("sort_by") = "" Then
.Value = "comp.companyname asc"
sort_by.Value = "comp.companyname"
direction.Value = "asc"
Else
.Value = Request("sort_by") & " " &
Request("direction")
sort_by.Value = Request("sort_by")
direction.Value = Request("direction")
End If
End With

cmdClients.CommandType = CommandType.StoredProcedure
cmdClients.Parameters.Add(New SqlParameter("@search",
sql_search))
cmdClients.Parameters.Add(prmOrderClient)

'use the command object to create a DataAdapter, fill the
dataset using the adapter and stored procedure
'set the repeater's datasource to the dataset so it can
populate, and bind it to use the retrieved info
Dim daclients As New SqlDataAdapter(cmdClients)
daclients.Fill(dsclients, "stp_registrar_clients")
Return dsclients.Tables("stp_registrar_clients").Copy

dsclients = Nothing
connection.Close()
'if the hidden field contains a value, then store it in
session, and redirect the page
'if the hidden field does not have a value, then no events will
fire.
If Not company_id.Value = "" Then
Session("company_name") = Request.Form("client_name")
Session("tempID") = Request.Form("company_id")
Response.Redirect("NextPage.aspx")
End If
End Function

Thanks for your help,
Matt

Nov 19 '05 #4

P: n/a
Ok, I see what you are doing now. Why don't you just change the current
page index? E.g. if you are changing the order to desc., you can change
the current page index to number of pages - current page index.

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

Nov 19 '05 #5

P: n/a
The currentpageindex is changed when the < or > button is pressed. Do
you mean change it in the doPaging function?

I think, though, if I'm reading your answer properly, you mean to do it
when I query the client listing. I don't know how that would work, as
I'm using a javascript function in the HTML to order the current page,
drawing from hidden field values.

I don't know if I've set myself up to run around in circles here, but
it's just escaping me.

Matt

Nov 19 '05 #6

P: n/a
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>&nbsp;&nbsp;&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a>&nbsp;&nbsp;")
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("&nbsp;&nbsp;")
End If

'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;<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" <ma*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
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.

Nov 19 '05 #7

P: n/a
Wow. Alright, this is something that I'm going to have to dissect on
my own time for this project, but reading the code makes absolute
sense. I didn't think of treating it as a grid.

I'm guessing (hoping?) that even though you used nested repeaters,
that this will work with only one. Like I said, I'll give it a shot
when I have a chance.

Very awesome of ou Joe. Thanks for this!

Matthew

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message news:<Oz*************@TK2MSFTNGP12.phx.gbl>...
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>&nbsp;&nbsp;&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a>&nbsp;&nbsp;")
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("&nbsp;&nbsp;")
End If

'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;<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" <ma*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
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.

Nov 19 '05 #8

P: n/a
You are welcome.
Print out each section of code and read over it.
It should be fairly straightforward.
Let me know if I omitted something. (Like a function call without including
the function.)

Once I got this working for 1 repeater adding a 2nd wasn't too hard.
The issue is that all the code sample on the web are for Datasets ad
Relations - but I need to use collections.
You can do some really cool things with nested repeaters. Each row of the
outer repeater runs once and then all the related rows of the inner repeater
run many times. (Think of using the outer one as a Header row for each
category and the inner one as the details for just that category.)
--
Joe Fallon
Access MVP

"Matthew Curiale" <ma*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
Wow. Alright, this is something that I'm going to have to dissect on
my own time for this project, but reading the code makes absolute
sense. I didn't think of treating it as a grid.

I'm guessing (hoping?) that even though you used nested repeaters,
that this will work with only one. Like I said, I'll give it a shot
when I have a chance.

Very awesome of ou Joe. Thanks for this!

Matthew

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:<Oz*************@TK2MSFTNGP12.phx.gbl>...
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>&nbsp;&nbsp;&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a>&nbsp;&nbsp;")
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("&nbsp;&nbsp;")
End If

'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;<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" <ma*****@gmail.com> wrote in message
news:a8**************************@posting.google.c om...
>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.

Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.