Text/waterswea
1. Program function: Implement paging for Repeater
2. Form design:
1. Create a new ASP.NET Web application, named Repeater2, and the save path is http://192.168.0.1/Repeater2 (Note: On my computer The IP of the website is 192.168.0.1 and the home directory is D:web folder) and click OK.
2. Add a table with three rows and one column to the form, add a Repeater control to the first row of the table, add two Label controls to the second row of the table, and add four Button buttons to the third row of the table.
3. Switch to the HTML code window and add the following code between <asp:Repeater id="Repeater1" runat="server"> and </asp:Repeater>:
<ItemTemplate>
<table id="Table2" style="FONT-SIZE: x-small" width="498">
<tr>
<td><%#DataBinder.Eval(Container,"DataItem.employeeid")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.lastname")%></td>
</tr>
</table>
</ItemTemplate>
3. Code design:
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Dim scon As New SqlConnection("server=localhost;database=northwind;uid=sa;pwd=123")
Dim sDA As SqlDataAdapter
Dim ds As DataSet
Dim currentPage As Integer 'Records which page you are currently on. Dim maxPage As Integer 'How many pages are there in total? Const rowCount As Integer = 3 'How many rows are there on one page? Dim rowSum As Integer 'How many rows are there in total
' Form code
omitPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New DataSet
Try
sDA.Fill(ds, "employees")
'Get the total number of rows rowSum = ds.Tables(0).Rows.Count
Catch ex As Exception
rowSum = 0
End Try
'If there is no data, exit the processIf rowSum = 0 Then Exit Sub
'Calculate the total number of pages of browsing dataIf rowSum Mod rowCount > 0 Then
'If there is a remainder, add 1
maxPage = rowSum rowCount + 1
Else
'Exactly divide maxPage = rowSum rowCount
End If
currentPage = 1
'Call the bound data process readpage(currentPage)
BindData()
Label2.Text = maxPage
'Home and previous page buttons are invisibleButton1.Visible = False
Button2.Visible = False
End If
End Sub
'Create a data-binding process
SubBindData()
Repeater1.DataSource = ds
Repeater1.DataBind()
Label1.Text = currentPage
End Sub
'Create a process to populate the data set
Sub readpage(ByVal n As Integer)
sDA = New SqlDataAdapter("select employeeid, lastname from employees order by employeeid", scon)
ds = New DataSet
ds.Clear()
sDA.Fill(ds, (n - 1) * rowCount, rowCount, "employees")
End Sub
'Home button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
currentPage = 1
'Call the process of filling the data set readpage(currentPage)
'Bind dataBindData()
'Set the homepage and first page buttons to be invisible, and display the next and last page buttons Button1.Visible = False
Button2.Visible = False
Button3.Visible = True
Button4.Visible = True
End Sub
'Previous page button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'If the current page is the second page, set the homepage and previous page buttons to be invisibleIf Label1.Text > 2 Then
Button3.Visible = True
Button4.Visible = True
Else
Button1.Visible = False
Button2.Visible = False
Button3.Visible = True
Button4.Visible = True
End If
currentPage = Label1.Text - 1
readpage(currentPage)
BindData()
End Sub
'Next page button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'If the current page is the penultimate page, set the last page and next page buttons to be invisibleIf Label1.Text < Label2.Text - 1 Then
Button1.Visible = True
Button2.Visible = True
Else
Button1.Visible = True
Button2.Visible = True
Button3.Visible = False
Button4.Visible = False
End If
currentPage = Label1.Text + 1
readpage(currentPage)
BindData()
End Sub
'Last page buttonPrivate Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Set the current page to the maximum number of pages currentPage = Label2.Text
readpage(currentPage)
BindData()
Button1.Visible = True
Button2.Visible = True
Button3.Visible = False
Button4.Visible = False
End Sub
End Class
form interface is as follows: