ASP.NET中Repeater控件实现分页功能

来源:互联网 发布:北师大文学院知乎 编辑:程序博客网 时间:2024/05/10 18:36

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 plaincopyprint?
  1. <SPAN style="COLOR: #333333">//repeate.aspx.cs
  2. using System;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Configuration;
  6. using System.Collections;
  7. using System.Web;
  8. using System.Web.Security;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Web.UI.HtmlControls;
  13. public partial class repeate : System.Web.UI.Page
  14. {
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. if (!IsPostBack)
  18. {
  19. Repeater1.DataSource = pds();
  20. Repeater1.DataBind();
  21. }
  22. }
  23. private PagedDataSource pds()
  24. {
  25. string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;
  26. SqlConnection con = new SqlConnection(connstring);
  27. DataSet ds = new DataSet();
  28. SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);
  29. sda.Fill(ds,"name");
  30. SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);
  31. sda2.Fill(ds,"title");
  32. ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);
  33. PagedDataSource pds = new PagedDataSource();
  34. pds.DataSource = ds.Tables["name"].DefaultView;
  35. pds.AllowPaging = true;//允许分页
  36. pds.PageSize = 5;//单页显示项数
  37. pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
  38. return pds;
  39. }
  40. protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
  41. {
  42. if (e.Item.ItemType == ListItemType.Footer)
  43. {
  44. DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");
  45. HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
  46. HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
  47. HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
  48. HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");
  49. pds().CurrentPageIndex = ddlp.SelectedIndex;
  50. int n = Convert.ToInt32(pds().PageCount);//n为分页数
  51. int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页
  52. Label lblpc = (Label)e.Item.FindControl("lblpc");
  53. lblpc.Text = n.ToString();
  54. Label lblp = (Label)e.Item.FindControl("lblp");
  55. lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
  56. if (!IsPostBack)
  57. {
  58. for (int j = 0; j < n; j++)
  59. {
  60. ddlp.Items.Add(Convert.ToString(j + 1));
  61. }
  62. }
  63. if (i <= 0)
  64. {
  65. lpfirst.Enabled = false;
  66. lpprev.Enabled = false;
  67. lplast.Enabled = true;
  68. lpnext.Enabled = true;
  69. }
  70. else
  71. {
  72. lpprev.NavigateUrl = "?page=" + (i - 1);
  73. }
  74. if (i >= n - 1)
  75. {
  76. lpfirst.Enabled = true;
  77. lplast.Enabled = false;
  78. lpnext.Enabled = false;
  79. lpprev.Enabled = true;
  80. }
  81. else
  82. {
  83. lpnext.NavigateUrl = "?page=" + (i + 1);
  84. }
  85. lpfirst.NavigateUrl = "?page=0";//向本页传递参数page
  86. lplast.NavigateUrl = "?page=" + (n - 1);
  87. ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
  88. }
  89. }
  90. protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
  91. {//脚模板中的下拉列表框更改时激发
  92. string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项
  93. Response.Redirect("repeate.aspx?page="+pg);//页面转向
  94. }
  95. }</SPAN><STRONG style="COLOR: rgb(255,0,0)">
  96. </STRONG>  
原创粉丝点击