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

ASP.NET程序中用Repeater实现分页,第1张

一 程序功能 为Repeater实现分页 二 窗体设计 新建ASP NET Web应用程序 命名为Repeater 保存路径为(注 我机子上的网站的IP是 的主目录是D:\web文件夹)然后点击确定 向窗体添加一个 行一列的表 向表的第一行中添加一个Repeater控件 向表的第二行中添加两个Label控件向表的第三行中添加四个Button按钮 切换到HTML代码窗口 在<asp:Repeater id= Repeater runat= server >和</asp:Repeater>之间添加以下代码 <ItemTemplate><table id= Table width= ><tr><td><%#DataBinder Eval(Container DataItem employeeid )%></td><td><%#DataBinder Eval(Container DataItem lastname )%></td></tr></table></ItemTemplate> 三 代码设计 Imports System Data SqlClientPublic Class WebForm Inherits System Web UI PageDim scon As New SqlConnection( server=localhostdatabase=northwinduid=sapwd= )Dim sDA As SqlDataAdapterDim ds As DataSetDim currentPage As Integer 记录著目前在哪一页上Dim maxPage As Integer 总共有多少页Const rowCount As Integer = 一页有多少行Dim rowSum As Integer 总共有多少行 窗体代码省略Private Sub Page_Load(ByVal sender As System Object ByVal e As System EventArgs) Handles MyBase LoadIf Not Page IsPostBack ThensDA = New SqlDataAdapter( select employeeid lastname from employees order by employeeid scon)ds = New DataSetTrysDA Fill(ds employees ) 获取总共有多少行rowSum = ds Tables( ) Rows CountCatch ex As ExceptionrowSum = End Try 如果没有数据 退出过程If rowSum = Then Exit Sub 计算出浏览数据的总页数If rowSum Mod rowCount > Then 有余数要加 maxPage = rowSum \ rowCount + Else 正好除尽maxPage = rowSum \ rowCountEnd IfcurrentPage = 调用绑定数据过程readpage(currentPage)BindData()Label Text = maxPage 首页和上一页按钮不可见Button Visible = FalseButton Visible = FalseEnd IfEnd Sub 创建一个绑定数据的过程Sub BindData()Repeater DataSource = dsRepeater DataBind()Label Text = currentPageEnd Sub 创建一个填充数据集的过程Sub readpage(ByVal n As Integer)sDA = New SqlDataAdapter( select employeeid lastname from employees order by employeeid scon)ds = New DataSetds Clear()sDA Fill(ds (n ) * rowCount rowCount employees )End Sub 首页按钮Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button ClickcurrentPage = 调用填充数据集过程readpage(currentPage) 绑定数据BindData() 设置首页 第一页按钮不可见 显示下一页尾页按钮Button Visible = FalseButton Visible = FalseButton Visible = TrueButton Visible = TrueEnd Sub 上一页按钮Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 如果现在页是第二页 设置首页和上一页按钮不可见If Label Text > ThenButton Visible = TrueButton Visible = TrueElseButton Visible = FalseButton Visible = FalseButton Visible = TrueButton Visible = TrueEnd IfcurrentPage = Label Text readpage(currentPage)BindData()End Sub 下一页按钮Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 如果现在页倒数第二页 设置最后页和下一页按钮不可见If Label Text < Label Text ThenButton Visible = TrueButton Visible = TrueElseButton Visible = TrueButton Visible = TrueButton Visible = FalseButton Visible = FalseEnd IfcurrentPage = Label Text + readpage(currentPage)BindData()End Sub 尾页按钮Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 设置当前页为最大页数currentPage = Label Textreadpage(currentPage)BindData()Button Visible = TrueButton Visible = TrueButton Visible = FalseButton Visible = FalseEnd SubEnd Class窗体界面如下所示 lishixinzhi/Article/program/net/201311/11338

datagrid 本身有自带的分页功能,代码如下:

Datagrid:

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged

DataGrid1.CurrentPageIndex = e.NewPageIndex

BindDG()

End Sub

添加信息的时候,就写入分页符,比如($)页面读取的时候,将内容切割放在数组中,就可以分开读取了.

如果要做好一点的,就后台添加新闻时生成静态页面的时候就分开,这样的话,读取速度快.

可以根据以下查询获得分页的数据,在将数据绑定到相应控件。

with temp

as(select ROW_NUMBER()over(order by 排序列名)as RowIndex,* from 表名)

select * from temp where RowIndex between 起始行号 and 结束行号

(注:起始行号、结束行号根据分页控件算出)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11471962.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存