一程序功能
为Repeater实现分页
二窗体设计
新建ASPNET Web应用程序命名为Repeater保存路径为(注我机子上的网站的IP是的主目录是D:\web文件夹)然后点击确定
向窗体添加一个行一列的表向表的第一行中添加一个Repeater控件向表的第二行中添加两个Label控件向表的第三行中添加四个Button按钮
切换到HTML代码窗口在<asp:Repeater id=Repeater runat=server>和</asp:Repeater>之间添加以下代码
<ItemTemplate>
<table id=Table width=>
<tr>
<td><%#DataBinderEval(ContainerDataItememployeeid)%></td>
<td><%#DataBinderEval(ContainerDataItemlastname)%></td>
</tr>
</table>
</ItemTemplate>
三代码设计
Imports SystemDataSqlClient
Public Class WebForm
Inherits SystemWebUIPage
Dim scon As New SqlConnection(server=localhost;database=northwind;uid=sa;pwd=)
Dim sDA As SqlDataAdapter
Dim ds As DataSet
Dim currentPage As Integer 记录着目前在哪一页上
Dim maxPage As Integer 总共有多少页
Const rowCount As Integer = 一页有多少行
Dim rowSum As Integer 总共有多少行
窗体代码省略
Private Sub Page_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
If Not PageIsPostBack Then
sDA = New SqlDataAdapter(select employeeid lastname from employees order by employeeid scon)
ds = New DataSet
Try
sDAFill(ds employees)
获取总共有多少行
rowSum = dsTables()RowsCount
Catch ex As Exception
rowSum =
End Try
如果没有数据退出过程
If rowSum = Then Exit Sub
计算出浏览数据的总页数
If rowSum Mod rowCount > Then
有余数要加
maxPage = rowSum \ rowCount +
Else
正好除尽
maxPage = rowSum \ rowCount
End If
currentPage =
调用绑定数据过程
readpage(currentPage)
BindData()
LabelText = maxPage
首页和上一页按钮不可见
ButtonVisible = False
ButtonVisible = False
End If
End Sub
创建一个绑定数据的过程
Sub BindData()
RepeaterDataSource = ds
RepeaterDataBind()
LabelText = currentPage
End Sub
创建一个填充数据集的过程
Sub readpage(ByVal n As Integer)
sDA = New SqlDataAdapter(select employeeid lastname from employees order by employeeid scon)
ds = New DataSet
dsClear()
sDAFill(ds (n ) * rowCount rowCount employees)
End Sub
首页按钮
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
currentPage =
调用填充数据集过程
readpage(currentPage)
绑定数据
BindData()
设置首页第一页按钮不可见显示下一页尾页按钮
ButtonVisible = False
ButtonVisible = False
ButtonVisible = True
ButtonVisible = True
End Sub
上一页按钮
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
如果现在页是第二页设置首页和上一页按钮不可见
If LabelText > Then
ButtonVisible = True
ButtonVisible = True
Else
ButtonVisible = False
ButtonVisible = False
ButtonVisible = True
ButtonVisible = True
End If
currentPage = LabelText
readpage(currentPage)
BindData()
End Sub
下一页按钮
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
如果现在页倒数第二页设置最后页和下一页按钮不可见
If LabelText < LabelText Then
ButtonVisible = True
ButtonVisible = True
Else
ButtonVisible = True
ButtonVisible = True
ButtonVisible = False
ButtonVisible = False
End If
currentPage = LabelText +
readpage(currentPage)
BindData()
End Sub
尾页按钮
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
设置当前页为最大页数
currentPage = LabelText
readpage(currentPage)
BindData()
ButtonVisible = True
ButtonVisible = True
ButtonVisible = False
ButtonVisible = False
End Sub
End Class
窗体界面如下所示