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

Asp.net提供了三个功能强大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分页功能。相对DataGrid,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。

实现DataList或Repeater控件的分页显示有几种方法:
1、写一个方法或存储过程,根据传入的页数返回需要显示的数据表(DataTable)
2、使用PagedDataSource类
     本篇文章主要说怎么使用PagedDataSource类实现DataList和Repeater控件的分页显示。DataGrid控件内部也使用了PagedDataSource类,PagedDataSource 类封装 DataGrid 控件的属性,这些属性使 DataGrid 可以执行分页。
PagedDataSource 类的部分公共属性:
AllowCustomPaging  获取或设置指示是否启用自定义分页的值。
AllowPaging   获取或设置指示是否启用分页的值。
Count    获取要从数据源使用的项数。
CurrentPageIndex   获取或设置当前页的索引。
DataSource   获取或设置数据源。
DataSourceCount   获取数据源中的项数。
FirstIndexInPage   获取页中的第一个索引。
IsCustomPagingEnabled  获取一个值,该值指示是否启用自定义分页。
IsFirstPage   获取一个值,该值指示当前页是否是首页。
IsLastPage   获取一个值,该值指示当前页是否是最后一页。
IsPagingEnabled   获取一个值,该值指示是否启用分页。
IsReadOnly   获取一个值,该值指示数据源是否是只读的。
IsSynchronized   获取一个值,该值指示是否同步对数据源的访问(线程安全)。
PageCount   获取显示数据源中的所有项所需要的总页数。
PageSize   获取或设置要在单页上显示的项数。
VirtualCount   获取或设置在使用自定义分页时数据源中的实际项数。
      这些属性是否和DataGrid的属性很相似?没错,DataGrid控件就是使用PagedDataSource类来实现数据分页显示的 。下面举个使用PagedDataSource类实现DataList和Repeater控件的分页显示的例子:

public void Page_Load(Object src,EventArgs e) 
 {
 OleDbConnection objConn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\test.mdb");
 OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Users",objConn);
 DataSet ds=new DataSet();
 objCommand.Fill(ds);
 //对PagedDataSource 对象的相关属性赋值
 PagedDataSource objPds = new PagedDataSource();
 objPds.DataSource = ds.Tables[0].DefaultView;
 objPds.AllowPaging = true;
 objPds.PageSize = 5;
 int CurPage;
 //当前页面从Page查询参数获取
 if (Request.QueryString["Page"] != null)
  CurPage=Convert.ToInt32(Request.QueryString["Page"]);
 else
  CurPage=1;
 objPds.CurrentPageIndex = CurPage-1; 
 lblCurrentPage.Text = "Page: " + CurPage.ToString();
 if (!objPds.IsFirstPage)
  lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
 if (!objPds.IsLastPage)
  lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);
 //把PagedDataSource 对象赋给Repeater控件
 Repeater1.DataSource=objPds;
 Repeater1.DataBind();
 }

这样就可以很简单的实现DataList和Repeater控件的分页显示。但这样做有个缺点,就是每次都要把所有页的数据Select出来,DataGrid也是这样,这样会降低一点效率(大多时候体会不出来差别);如果使用第一种方法就可以只Select出当前页的数据。

Asp.net提供了三个功能强大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分页功能。相对DataGrid,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。

PagedDataSource 类封装 DataGrid 控件的属性,这些属性使 DataGrid 可以执行分页。
PagedDataSource 类的部分公共属性:

AllowCustomPaging获取或设置指示是否启用自定义分页的值。
AllowPaging 获取或设置指示是否启用分页的值。
Count 获取要从数据源使用的项数。
CurrentPageIndex 获取或设置当前页的索引。
DataSource 获取或设置数据源。
DataSourceCount 获取数据源中的项数。
FirstIndexInPage 获取页中的第一个索引。
IsCustomPagingEnabled获取一个值,该值指示是否启用自定义分页。
IsFirstPage 获取一个值,该值指示当前页是否是首页。
IsLastPage 获取一个值,该值指示当前页是否是最后一页。
IsPagingEnabled 获取一个值,该值指示是否启用分页。
IsReadOnly 获取一个值,该值指示数据源是否是只读的。
IsSynchronized 获取一个值,该值指示是否同步对数据源的访问(线程安全)。
PageCount 获取显示数据源中的所有项所需要的总页数。
PageSize 获取或设置要在单页上显示的项数。
VirtualCount 获取或设置在使用自定义分页时数据源中的实际项数。

DataGrid控件就是使用PagedDataSource类来实现数据分页显示的,所以DataList和Repeater也同样可以使用PagedDataSource来显示分页。

VB.NET CODE

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在這裡放置使用者程式碼以初始化網頁
        If Not IsPostBack Then
            pds.DataSource = Me.CreateDataSource
            pds.AllowPaging = True
            pds.PageSize = 4
            intTotalRec = Me.CreateDataSource.Count
            pds.CurrentPageIndex = 0
            'Response.Write(" ")
            intTotalPage = pds.PageCount.ToString
            Me.DataList1.DataSource = pds
            Me.DataList1.DataBind()
            Me.showinfo()
            'Session("pc") = "1"
        Else
            '含有Session的注釋代碼表示在頁面驅動了一個事件後
            '會先執行page_load事件然後執行所對應的的事件.
            'Session("pc") = Session("pc") + "2" 
            'Response.Write(" ")
            pds.DataSource = Me.CreateDataSource
            pds.AllowPaging = True
            pds.PageSize = 4
            intTotalRec = Me.CreateDataSource.Count
            'Response.Write(" ")
            intTotalPage = pds.PageCount.ToString
        End If
    End Sub

    Public Function CreateDataSource() As DataView
        Dim dt As DataTable
        Dim dr As DataRow

        dt = New DataTable
        dt.Columns.Add("Integer", GetType(Integer))
        dt.Columns.Add("String", GetType(String))

        Dim i As Integer
        For i = 0 To 5
            dr = dt.NewRow
            dr.Item(0) = i
            dr.Item(1) = "item " & i.ToString
            dt.Rows.Add(dr)
        Next

        CreateDataSource = dt.DefaultView
    End Function

    Private Sub lbFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbFirst.Click
        'Session("pc") = Session("pc") + "3"
        'Response.Write(" ")

        pds.CurrentPageIndex = 0
        Me.DataList1.DataSource = pds
        Me.DataList1.DataBind()
        Me.showinfo()
    End Sub

    Private Sub lbLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbLast.Click
        pds.CurrentPageIndex = Me.intTotalPage - 1
        Me.DataList1.DataSource = pds
        Me.DataList1.DataBind()
        Me.showinfo()
    End Sub

    Private Sub lbPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbPrevious.Click
        If pds.CurrentPageIndex = 0 Then
        Else
            pds.CurrentPageIndex -= 1
        End If
        Me.DataList1.DataSource = pds
        Me.DataList1.DataBind()
        Me.showinfo()
    End Sub

    Private Sub lbNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbNext.Click
        If pds.CurrentPageIndex = pds.PageCount - 1 Then
        Else
            pds.CurrentPageIndex += 1
        End If
        Me.DataList1.DataSource = pds
        Me.DataList1.DataBind()
        Me.showinfo()
    End Sub

    Private Sub showinfo()
        Me.lInfo.Text = (pds.CurrentPageIndex + 1).ToString & "/" & Me.intTotalPage.ToString
    End Sub


Note:

需要給PagedDataSource賦一個數據源,同時還要設定允許分頁(AllowPaging)、每頁的大小(PageSize),這樣PagedDataSource對象就可以自動算出頁數(PageCount),然後就可以把這個PagedDataSource作為數據源賦於一個DataList對象,最後將DataList對象綁定。


       您对本文的宝贵意见:
       
感谢您的鼓励和批评,它将是我进步的动力

原文地址:https://www.cnblogs.com/Leo_wl/p/1761965.html