I have been looking and looking trying to make this paging work, and no such luck.
I've tried many tutorials online and I think I found one that's really close to being right.
The tutorials original code is:
<table border="1">
<asp:repeater id="rptParts" runat="server">
<itemtemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "PartID") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Quantity") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Manufacturer") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Color") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Price") %>
</td>
</tr>
</itemtemplate>
</asp:repeater>
<tr>
<td>
<asp:button id="cmdPrev" runat="server" text="Prev"
onclick="cmdPrev_Click"></asp:button>
<asp:button id="cmdNext" runat="server" text="Next"
onclick="cmdNext_Click"></asp:button>
</td>
</tr>
</table>
Private _CurrentPage As Integer
Public Property CurrentPage() As Integer
Get
'look for current page in ViewState
_CurrentPage = CInt(ViewState("CurrentPage"))
Return _CurrentPage
End Get
Set(ByVal Value As Integer)
viewstate("CurrentPage") = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
BindData()
End Sub
Sub BindData()
'Read sample item info from XML document into a DataSet
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath(".") & "\..\Xml\PartList.xml")
' Populate the repeater control with the Items DataSet
Dim dsPaged As New PagedDataSource()
dsPaged.DataSource = ds.Tables(0).DefaultView
dsPaged.AllowPaging = True
dsPaged.PageSize = 5
dsPaged.CurrentPageIndex = CurrentPage
'Disable Prev or Next buttons if necessary
cmdPrev.Enabled = Not dsPaged.IsFirstPage
cmdNext.Enabled = Not dsPaged.IsLastPage
rptParts.DataSource = dsPaged
rptParts.DataBind()
'Dispose Items
ds.Dispose()
End Sub
Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Set viewstate variable to the previous page
CurrentPage -= 1
' Reload control
BindData()
End Sub
Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Set viewstate variable to the previous page
CurrentPage += 1
' Reload control
BindData()
End Sub
Right away it has an error with 2.0 for DataSet, so I changed it to Data.DataSet.
Then it looks like it's doing fine, but once loaded it keeps having an error as below.
Cannot find table 0.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Cannot find table 0.
Source Error:
Line 308: ' Populate the repeater control with the Items DataSet
Line 309: Dim dsPaged As New PagedDataSource()
Line 310: dsPaged.DataSource = ds.Tables(0).DefaultView
Line 311: dsPaged.AllowPaging = True
Line 312: dsPaged.PageSize = 2
|
I can not figure out what to do with that line, no errors if I delete it, but it doesn't work then.
Any help would be appreciated, or if anyone has any code for a working paging system please post it.
Thank you in advance.
Be sure to visit
www.detelli.comAnd please remember to click ?Mark as Answer? on the post that helps you.
This can be beneficial to other community members reading the thread.