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

ASP.NET
为T-SQL添加intellisense功能
SQL Server 2005安装过程中出现错误的解决办法
SQL Server 2005 RTM 安装错误 :The SQL Server System Configuration Checker cannot be executed due to
有关于JSON的一些资料
不能忽略c#中的using和as操作符的用处
JavaScript系列之―同步还是异步?
获取远程网页的内容之一(downmoon原创)
获取远程网页的内容之二(downmoon原创)
ASP.Net中防止刷新自动触发事件的解决方案
asp.net下用js实现鼠标移至小图,自动显示相应大图
Asp.Net 和 AJAX.Net 的区别
提交页面的定位--scrollIntoView的用法
利用AJAX与数据岛实现无刷新绑定
asp.net下判断用户什么时候离开,以什么方式离开
DataSet 添加数据集、行、列、主键和外键等操作示例
读写xml所有节点个人小结 和 读取xml节点的数据总结
收藏的asp.net文件上传类源码
asp.net下GDI+的一些常用应用(水印,文字,圆角处理)技巧
一个可以让.net程序在非WIN平台上运行的软件Mono
使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 175 ::
收藏到网摘: 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

窗体界面如下所示: