当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET程序中用Repeater实现分页

ASP.NET
利用ASP.NET和AJAX解决手工拼接HTML问题
Asp.net关于动态输出服务器控件的应用
技巧/诀窍:在ASP.NET中重写URL
ASP.NET 自定义控件从入门到精通3
以Post方式向网页发送数据
ASP.NET实现数据采集
使用ASP.NET Image Generation生成图片缩略图及水印
ASP.NET安全问题--ASP.NET安全架构
反思软件系统与软件系统之间的集成交互问题
.Net实现程序的插件机制
作为ASP.NET开发人员必须养成的编程习惯
总结了一下ADO.NET数据库连接的相关知识
VB.NET中有用的通用对象列表
HTTP Error 503与.NET 3.5 SP1 X64
ASP.NET创建Web服务之使用事务
ASP.NET中基类Page_Load方法后执行原因分析
ASP.NET中让网页弹出窗口不再困难
改变.net网站的默认解决方案位置
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之二
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之一

ASP.NET程序中用Repeater实现分页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 115 ::
收藏到网摘: n/a

文/waterswea

一、程序功能:为Repeater实现分页

二、窗体设计:

1、新建ASP.NETWeb应用程序,命名为Repeater2,保存路径为http://192.168.0.1/Repeater2(注:我机子上的网站的IP是192.168.0.1的主目录是D:\web文件夹)然后点击确定。

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

窗体界面如下所示: