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开始。
下面我们来看一个例子:
前台的代码:
下面的是后台的代码:
[csharp] view plain copy print ?
- <SPAN style="COLOR: #333333">
- using 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);
- int i = Convert.ToInt32(pds().CurrentPageIndex);
- 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";
- lplast.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);
- }
- }</SPAN><STRONG style="COLOR: rgb(255,0,0)">
- </STRONG>