473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting and Paging in a repeater

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
8 4127
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave S | last post by:
Hi, I'm using a datagrid to display contents of a dataset and I've set the datagrid to allow paging and sorting. Rather than re-retrieve from the DB, I store the original dataset in a Session...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Hajime Kusakabe | last post by:
Hi. I have created a datagrid (datagrid1) without any columns on a aspx page. Then aspx.vb adds columns from a database. It is somthing like this .... ================================== Dim...
2
by: Mark | last post by:
I am attempting to build a repeating list using a repeater control. I want to add a checkbox to each item (line) and 'Select All' and 'Clear All' buttons. I have figured out how to do this...
1
by: Guadala Harry | last post by:
I understand that paging is a built-in feature of the DataGrid, and that paging is not built-in to other listing controls such as the DataList and the Repeater. I need to implement paging...
1
by: Guoqi Zheng | last post by:
Sir, The default paging of datagrid is somehow use too much resource, so I am using Stored procedure for the paging. You can find my Stored procedure at the end of this message. It works...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
2
by: Jeff | last post by:
Hey ASP.NET 2.0 GridView have AllowPaging & PageSize for the letting the GridView span multiple pages. But is the same allowed on a Repeater control? I didn't see any properties like...
1
by: John A Grandy | last post by:
In regard to a GridView that must support searching, filtering, sorting, and paging ... There is a tradeoff in performing the sorting and paging in the database versus to creating a CLR sort...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.