关于ASP.NET List泛型分页实例

作者:袖梨 2022-06-25

我们以前做ASP.NET网站,通常是把DataSet绑定到显示控件上,再用各种手段进行分页,比如PagedDataSource。然而,如果你是从业务层取数据,通常是一个List,这个时候就不能用以往的方法分页了。

一种办法是用ObjectDataSource,绑定GridView、ListView就可以分页,但如果遇到DataList或Repeater,就得自己写分页了。下面是我昨天写的List泛型分页Demo,有图有真相:

下面发代码:

List泛型分页通用类:

 代码如下 复制代码

public class ListPager : List{
    private int _CurrentIndex;
    private int _PageSize;
    private int _TotalItem;
    private int _PageCount;

    public int CurrentIndex
    {
        get { return _CurrentIndex; }
        set { _CurrentIndex = value; }
    }

    public int PageSize
    {
        get { return _PageSize; }
        set { _PageSize = value; }
    }

    public int TotalItem
    {
        get { return _TotalItem; }
        set { _TotalItem = value; }
    }

    public int PageCount
    {
        get { return _PageCount; }
        set { _PageCount = value; }
    }

    public ListPager(List list, int index, int pageSize)
    {
        this._CurrentIndex = index;
        this._PageSize = pageSize;

        int startIndex = (this._CurrentIndex - 1) * PageSize;
        for (int i = startIndex; i < startIndex + this._PageSize && i < list.Count; i++)
        {
            this.Add(list[i]);
        }

        this._TotalItem = list.Count;
        this._PageCount = (this._TotalItem / PageSize) + 1;
    }
}

测试用Model和模拟的Bll:

 代码如下 复制代码

public partial class NewsModel
{
    int _Id;
    string _Title;
    string _Content;
    DateTime _Posttime;

    public int Id
    {
        get { return _Id; }
        set { _Id = value; }
    }

    public string Title
    {
        get { return _Title; }
        set { _Title = value; }
    }

    public string Content
    {
        get { return _Content; }
        set { _Content = value; }
    }

    public DateTime Posttime
    {
        get { return _Posttime; }
        set { _Posttime = value; }
    }
}

ASP.NET页面代码(部分):

 代码如下 复制代码


    ASP.NET泛型分页



   


        Repeater控件分页Demo


   

            <asp:Repeater ID="RptNews" runat="server">
               
                   

  •                     <%# ((Model.NewsModel)Container.DataItem).Id %>,
                        <%# ((Model.NewsModel)Container.DataItem).Title %>,
                        <%# ((Model.NewsModel)Container.DataItem).Content %>,
                        <%# ((Model.NewsModel)Container.DataItem).Posttime %>

  •            

           
       

   

       
       
       
       
        /
       
        页
       
       
   

ASP.NET后台代码:

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Web.UI;
using Common;

namespace Web
{
    public partial class _Default : System.Web.UI.Page
    {
        private static ListPager lp = null;
        private static int currentIndex = 1;
        private static int pageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRepeater(1, pageSize);
            }
        }

        private void BindRepeater(int index, int pageSize)
        {
            BLLDemo.NewsBll optNews = new BLLDemo.NewsBll();
            List list = optNews.GetModels();

            lp = new ListPager(list, index, pageSize);

            RptNews.DataSource = lp;
            RptNews.DataBind();

            BindPagerControls();
        }

        private void BindPagerControls()
        {
            // 判断上一页,下一页按钮是否启用.
            btnPrevious.Enabled = currentIndex != 1;
            btnNext.Enabled = currentIndex != lp.PageCount;

            lblCurrentPage.Text = lp.CurrentIndex.ToString();
            lblTotalPage.Text = lp.PageCount.ToString();
            lblPageInfo.Text = String.Format("共{0}条记录,每页显示{1}条", lp.TotalItem, lp.PageSize);
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            --currentIndex;
            BindRepeater(currentIndex, pageSize);
            BindPagerControls();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ++currentIndex;
            BindRepeater(currentIndex, pageSize);
            BindPagerControls();
        }

        protected void btnFirstPage_Click(object sender, EventArgs e)
        {
            currentIndex = 1; //修正为1,第一次发的时候写成0了,2B了~
            BindRepeater(currentIndex, pageSize);
        }

        protected void btnLastPage_Click(object sender, EventArgs e)
        {
            currentIndex = lp.PageCount;
            BindRepeater(currentIndex, pageSize);
        }
    }
}

相关文章

精彩推荐