Repeater
轻量级,完全的自定义

Repeater分页,需要依靠PagedDataSource。这个类存在于System.Web.UI.WebControls命名空间。它的作用是作为数据源与数据显示控件的中间介质。如:

  数据源->PagedDataSource->数据绑定控件

之间的关系通过以下代码来实现:

  PagedDataSource pds=new PagedDataSource ();

  pds.DataSource=dataTable;

  repeater1.DataSource=pds;

  repeater1.DataBind();

  三者之间发生关系的代码就是这些。

  那么PagedDataSource又是怎样工作的呢?MSDN上面也没有讲。这仅仅是我的推断。

  PagedDataSource封装了从底层数据源(如:DataTable)中取出第几页数据的中间过程。我们只需设置

  PagedDataSource .AllowPaging=true;

  PagedDataSource .PageSize=xx;

  PagedDataSource.CurrentPageIndex=currentPage;

  就可以取出指定页的数据,而数据绑定控件将从PagedDataSource 中获取这些数据以显示。PagedDataSource 在这里成一个中介。而数据绑定组件如何取数据,PagedDataSource 是如何分页,并取出对应的数据,这个是asp.net框架内部实现,对我们完全透明。

  另一个需要着重讲的是,PagedDataSource 中页号是从0开始的,并不是从1开始。

下面我们来看一个例子:

前台的代码:

//repeate.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeate.aspx.cs" Inherits="repeate" %>
<%@ Import Namespace="System.Data" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>无标题页</title>
</head>
<body><form id="form1" runat="server"><div><asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" ><HeaderTemplate><%-- 我是头模板--%><table width="500"><tr style="background-color: #ccffcc;"><td>作者</td><td>书籍</td></tr></HeaderTemplate><ItemTemplate><%--我是项模板--%><tr><td><a href='repeate.aspx?id=<%# Eval("au_id")%>'><%# Eval("au_lname") %></a></td><td><asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Eval("myrela") %>'><ItemTemplate><%# Eval("[/"title_id/"]") %></ItemTemplate></asp:Repeater></td></tr>        </ItemTemplate><SeparatorTemplate><%--这是分隔线模板--%><tr><td colspan="2"><hr style="border-top:1pt;"/></td></tr></SeparatorTemplate><FooterTemplate><%--这是脚模板--%><tr><td colspan="2" style="font-size:12pt;color:#0099ff; background-color:#e6feda;">共<asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>页 当前为第<asp:Label ID="lblp" runat="server" Text="Label"></asp:Label>页<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink><asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink><asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink><asp:HyperLink ID="hlla" runat="server" Text="尾页"></asp:HyperLink>跳至第<asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" ></asp:DropDownList>页</td></tr></table></FooterTemplate></asp:Repeater></div></form>
</body>
</html>

下面的是后台的代码:

//repeate.aspx.csusing System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class repeate : System.Web.UI.Page
{protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){Repeater1.DataSource = pds();Repeater1.DataBind();            }}private PagedDataSource pds(){string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;SqlConnection con = new SqlConnection(connstring);DataSet ds = new DataSet();SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);sda.Fill(ds,"name");SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);sda2.Fill(ds,"title");ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);PagedDataSource pds = new PagedDataSource();pds.DataSource = ds.Tables["name"].DefaultView;pds.AllowPaging = true;//允许分页pds.PageSize = 5;//单页显示项数pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);return pds;}protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e){if (e.Item.ItemType == ListItemType.Footer){DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");pds().CurrentPageIndex = ddlp.SelectedIndex;int n = Convert.ToInt32(pds().PageCount);//n为分页数int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页Label lblpc = (Label)e.Item.FindControl("lblpc");lblpc.Text = n.ToString();Label lblp = (Label)e.Item.FindControl("lblp");lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);if (!IsPostBack){for (int j = 0; j < n; j++){ddlp.Items.Add(Convert.ToString(j + 1));}}if (i <= 0){lpfirst.Enabled = false;lpprev.Enabled = false;lplast.Enabled = true;lpnext.Enabled = true;}else{lpprev.NavigateUrl = "?page=" + (i - 1);}if (i >= n - 1){lpfirst.Enabled = true;lplast.Enabled = false;lpnext.Enabled = false;lpprev.Enabled = true;}else{lpnext.NavigateUrl = "?page=" + (i + 1);}lpfirst.NavigateUrl = "?page=0";//向本页传递参数pagelplast.NavigateUrl = "?page=" + (n - 1);ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号}}protected void ddlp_SelectedIndexChanged(object sender, EventArgs e){//脚模板中的下拉列表框更改时激发string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项Response.Redirect("repeate.aspx?page="+pg);//页面转向}
}

ASP.NET中Repeater控件实现分页功能相关推荐

  1. C# ASP.NET中Repeater控件

    最主要的用途是可以将数据依照编程者所指定的自定义格式逐一显示出来.只要将想要展示的格式先定好,它就会按照指定的格式来显示:这个预定好的格式称为"模板"(Template). 通过页 ...

  2. aspx repeater 用法_ASP.NET中repeater控件用法实例

    本文实例讲述了ASP.NET中repeater控件用法.分享给大家供大家参考.具体实现方法如下: repeater绑定数据: protected void Page_Load(object sende ...

  3. Datalist控件,Repeater控件如何分页?

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...

  4. 读书笔记:《Aspx开发200问》——如何实现Repeater控件的分页

    由于Repeater控件没有分页相关的属性,要使用System.Web.UI.WebControl中的PageDataSource类. PageDataSource封装了DataGrid控件的分页属性 ...

  5. 使用PagedDataSource类实现DataList和Repeater控件的分页显示

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...

  6. asp.NET中 treeview 控件的使用

    asp.NET中 treeview 控件的使用 treeview控件实现点击Node,弹出相应WebUserControl画面 所需控件 treeview控件 WebUserControl,web用户 ...

  7. ASP.NET Web开发中Repeater控件的使用

    在ASP.NET中数据绑定是其提供的访问数据库的方法,数据控件则是用来显示从数据库中获取的数据. 首先讲下待会要用到的属性和方法: DataBind():显示绑定的数据 DataSource:指定数据 ...

  8. Repeater控件的分页问题

    作者:zhoubin@mail.sdu.edu.cn 以前做ASP的时间不算短,可是做ASP.NET我是个新手.前几天做项目,遇到一个问题,要求比较复杂的数据格式显示,用DataGrid非常难实现,后 ...

  9. aspx repeater 用法_asp.net中repeater控件用法笔记

    大家可能都对 datagrid 比较熟悉,但是如果在数据量大的时候,我们就得考虑使用 repeater 作为我们的数据绑定控件了. repeater 控件与 datagrid ( 以及 datalis ...

最新文章

  1. 十二、进程的同步与互斥
  2. 点击某个a标签,禁止页面自动跳转到该页面的顶部
  3. 互联网医疗上市“大逃杀”
  4. [Objective-c 基础 - 2.1] 封装
  5. 使用Dagger 2在GWT中进行依赖注入
  6. React开发(255):react项目理解 ant design 注意报错提示
  7. C#面向对象名词比较(三)
  8. excel表格vlookup函数怎么用_只会Vlookup函数Out了!Excel所有查找公式全在这儿(共16大类)...
  9. 倾斜摄影和近景摄影技术
  10. Java并发教程(Oracle官方资料) 分享
  11. 【LibTorch】Microsoft C++ 异常: c10::NotImplementedError,位于内存位置 0x000000E8A9DAEDC0 处。
  12. 十一则:程序员冷“笑话”据说只有真正的程序员才看得懂
  13. 我是如何一步步获取房东的WiFi后台管理密码的【社工思路】
  14. SystemUI 人脸识别解锁后指纹消耗次数并没有清楚
  15. windows10 BitLocker恢复秘钥(此方法只适用于使用Microsoft账号登录系统用户)
  16. 运动无线蓝牙耳机推荐、运动健身必备的运动耳机
  17. Web案例——制作简历
  18. IOS Appstore 预览图尺寸
  19. 不同分辨率对应的像素输出时钟以及同步信号参数的整理
  20. 2021-2027全球与中国猫罐头市场现状及未来发展趋势

热门文章

  1. Frp内网端口映射教程
  2. zoj 1543 Stripies
  3. 三款国产宝藏级别软件,个个功能强大且好用,能让你的电脑黑科技感拉满
  4. 一夜爆红的4款国产软件,却一度被大众误以为是外国人开发
  5. C++的Static函数
  6. 光辉城市产品Api摸索
  7. c语言单链表存储字符串,用链表的形式存储一个字符串 按正序和逆序输出字符串(数据结构考试)...
  8. IDEA自动导入包,自动删除无用包
  9. 新浪云 股票实时筛选系统 简介
  10. 塔王之王获取服务器信息0%,《塔王之王》新手最全攻略(全) 受益的来顶