文/waterswea
一、程序功能:为Repeater实现分页
二、窗体设计:
1、新建ASP.NETWeb应用程序,命名为Repeater2,保存路径为
2、向窗体添加一个3行一列的表,向表的第一行中添加一个Repeater控件,向表的第二行中添加两个Label控件向表的第三行中添加四个Button按钮。
3、切换到HTML代码窗口,在<asp:Repeaterid="Repeater1"runat="server">和</asp:Repeater>之间添加以下代码:
<ItemTemplate>
<tableid="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>
三、代码设计:
ImportsSystem.Data.SqlClient
PublicClassWebForm1
InheritsSystem.Web.UI.Page
DimsconAsNewSqlConnection("server=localhost;database=northwind;uid=sa;pwd=123")
DimsDAAsSqlDataAdapter
DimdsAsDataSet
DimcurrentPageAsInteger'记录着目前在哪一页上
DimmaxPageAsInteger'总共有多少页
ConstrowCountAsInteger=3'一页有多少行
DimrowSumAsInteger'总共有多少行
'窗体代码省略
PrivateSubPage_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
IfNotPage.IsPostBackThen
sDA=NewSqlDataAdapter("selectemployeeid,lastnamefromemployeesorderbyemployeeid",scon)
ds=NewDataSet
Try
sDA.Fill(ds,"employees")
'获取总共有多少行
rowSum=ds.Tables(0).Rows.Count
CatchexAsException
rowSum=0
EndTry
'如果没有数据,退出过程
IfrowSum=0ThenExitSub
'计算出浏览数据的总页数
IfrowSumModrowCount>0Then
'有余数要加1
maxPage=rowSum\rowCount+1
Else
'正好除尽
maxPage=rowSum\rowCount
EndIf
currentPage=1
'调用绑定数据过程
readpage(currentPage)
BindData()
Label2.Text=maxPage
'首页和上一页按钮不可见
Button1.Visible=False
Button2.Visible=False
EndIf
EndSub
'创建一个绑定数据的过程
SubBindData()
Repeater1.DataSource=ds
Repeater1.DataBind()
Label1.Text=currentPage
EndSub
'创建一个填充数据集的过程
Subreadpage(ByValnAsInteger)
sDA=NewSqlDataAdapter("selectemployeeid,lastnamefromemployeesorderbyemployeeid",scon)
ds=NewDataSet
ds.Clear()
sDA.Fill(ds,(n-1)*rowCount,rowCount,"employees")
EndSub
'首页按钮
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
currentPage=1
'调用填充数据集过程
readpage(currentPage)
'绑定数据
BindData()
'设置首页、第一页按钮不可见,显示下一页尾页按钮
Button1.Visible=False
Button2.Visible=False
Button3.Visible=True
Button4.Visible=True
EndSub
'上一页按钮
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
'如果现在页是第二页,设置首页和上一页按钮不可见
IfLabel1.Text>2Then
Button3.Visible=True
Button4.Visible=True
Else
Button1.Visible=False
Button2.Visible=False
Button3.Visible=True
Button4.Visible=True
EndIf
currentPage=Label1.Text-1
readpage(currentPage)
BindData()
EndSub
'下一页按钮
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
'如果现在页倒数第二页,设置最后页和下一页按钮不可见
IfLabel1.Text<Label2.Text-1Then
Button1.Visible=True
Button2.Visible=True
Else
Button1.Visible=True
Button2.Visible=True
Button3.Visible=False
Button4.Visible=False
EndIf
currentPage=Label1.Text+1
readpage(currentPage)
BindData()
EndSub
'尾页按钮
PrivateSubButton4_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton4.Click
'设置当前页为最大页数
currentPage=Label2.Text
readpage(currentPage)
BindData()
Button1.Visible=True
Button2.Visible=True
Button3.Visible=False
Button4.Visible=False
EndSub
EndClass
窗体界面如下所示: