Implement paging using Repeater in ASP. NET programs

Source: Internet
Author: User

I. program functions:
Implement Paging for Repeater
  
  Ii. Form Design:
1. Create an ASP. NET Web application, named Repeater2, and saved to http: // 192.168.0.1/Repeater2 (note: the IP address of the website on my host is the home directory of 192.168.0.1 and the d: \ web Folder) click OK.
  
2. Add a table with three rows and one column to the form, and add a Repeater control to the first row of the table, add two Label controls to the second row of the table to add four 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>
  
  Iii. 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 the current page
Dim maxPage As Integer 'total number of pages
Const rowCount As Integer = 3'
Dim rowSum As Integer 'total number of rows
  
'Form Code omitted
  
Private 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
  
'Exit the process if no data exists.
If rowSum = 0 Then Exit Sub
'Calculate the total page number of browsed data
If rowSum Mod rowCount> 0 Then
'Add 1 to the remainder
MaxPage = rowSum \ rowCount + 1
Else
'Just remove
MaxPage = rowSum \ rowCount
End If
  
CurrentPage = 1
'Call the data binding process
Readpage (currentPage)
BindData ()
Label2.Text = maxPage
'Homepage and previous page buttons are invisible
Button1.Visible = False
Button2.Visible = False
End If
End Sub
  
'Create a data binding process
Sub BindData ()
Repeater1.DataSource = ds
Repeater1.DataBind ()
Label1.Text = currentPage
End Sub
  
'Create a dataset Filling Process
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
  
'Homepage buttons
Private Sub button#click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
  
CurrentPage = 1
'Call the filling dataset Process
Readpage (currentPage)
'Bind data
BindData ()
'Set the homepage and the button on the first page are invisible. The button on the last page of the next page is displayed.
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 home page and the previous page button to be invisible
If 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 last and last pages of the current page are not visible
If 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' button
Private 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
  
The form interface is as follows:
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.