2011.11.13修正:

1. 改正页数计算方法,以前的计算公式遇到整除就会多一页

2. 重发关键代码,以前因为编辑器的原因,部分代码会被过滤

3. 新增“转到[]页”功能

我们以前做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; }
    }

    ///
    /// 构造函数
    /// 
    ///要分页的List泛型
    ///起始页码
    ///分页大小
    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;
        this.PageCount = (this._TotalItem + this.PageSize - 1) / this._PageSize;
    }
}

测试用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; }
    }
}
public class NewsBll
{
    public List GetModels()
    {
        List list = new List()
        {
            new NewsModel(){ Id = 1, Content = "abcdef", Posttime = DateTime.Now, Title = "abc" },
            new NewsModel(){ Id = 2, Content = "zxcvbn", Posttime = DateTime.Now, Title = "def" },
            new NewsModel(){ Id = 3, Content = "qwerty", Posttime = DateTime.Now, Title = "ghi" },
            new NewsModel(){ Id = 4, Content = "pijpas", Posttime = DateTime.Now, Title = "jkl" },
            new NewsModel(){ Id = 5, Content = "geegwg", Posttime = DateTime.Now, Title = "mno" },
            new NewsModel(){ Id = 6, Content = "jiojio", Posttime = DateTime.Now, Title = "pqr" },
            new NewsModel(){ Id = 7, Content = "zpppaf", Posttime = DateTime.Now, Title = "stu" },
            new NewsModel(){ Id = 8, Content = "1pijqs", Posttime = DateTime.Now, Title = "vwx" },
            new NewsModel(){ Id = 9, Content = "1pijqs", Posttime = DateTime.Now, Title = "vwx" },
            new NewsModel(){ Id = 10, Content = "twetwte", Posttime = DateTime.Now, Title = "yza" },
            new NewsModel(){ Id = 11, Content = "qewqrzxf", Posttime = DateTime.Now, Title = "bcd" }
        };
        return list;
    }
}

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

<h2>
    ASP.NET泛型分页</h2>
<div>
    <h3>
        Repeater控件分页Demo</h3>
    <ul>
        <asp:Repeater ID="RptNews" runat="server">
            <ItemTemplate>
                <li>
                    <%# ((Model.NewsModel)Container.DataItem).Id %>,
                    <%# ((Model.NewsModel)Container.DataItem).Title %>,
                    <%# ((Model.NewsModel)Container.DataItem).Content %>,
                    <%# ((Model.NewsModel)Container.DataItem).Posttime %></li>
            </ItemTemplate>
        </asp:Repeater>
    </ul>
    <div>
        <asp:Label ID="lblPageInfo" runat="server" Text="PageSize"></asp:Label>
        <asp:Button ID="btnFirstPage" runat="server" Text="首页" OnClick="btnFirstPage_Click"/>
        <asp:Button ID="btnPrevious" runat="server" Text="上一页" OnClick="btnPrevious_Click"/>
        <asp:Label ID="lblCurrentPage" runat="server" Text="Current"></asp:Label>
        /
        <asp:Label ID="lblTotalPage" runat="server" Text="Total"></asp:Label>
        页
        <asp:Button ID="btnNext" runat="server" Text="下一页" OnClick="btnNext_Click"/>
        <asp:Button ID="btnLastPage" runat="server" Text="尾页" OnClick="btnLastPage_Click"/>
    </div>
</div>

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);
        }
    }
}

转到[]页功能:

ASPX代码:

<asp:textbox id="txtGotoPage" runat="server" cssclass="textbox" width="30px"></asp:textbox>
<asp:button id="btnGotoPage" runat="server" text="转到" cssclass="aspbtn" onclick="btnGotoPage_Click"></asp:button>

C#代码:(WYJ.Web.Utils为我的工具类,可以换成你自己的)

protected void btnGotoPage_Click(object sender, EventArgs e)
{
    int pageIndex = 1;
    try
    {
        pageIndex = Convert.ToInt32(txtGotoPage.Text.Trim());
        if (pageIndex < 1 || pageIndex > lp.PageCount)
        {
            throw new Exception("页码范围不正确!");
        }
        else
        {
            currentIndex = pageIndex;
            ReadData(currentIndex, pageSize);
        }
    }
    catch (InvalidCastException)
    {
        WYJ.Web.Utils.MessageBox.Show(this, "页码必须是数字。");
    }
    catch (Exception ex)
    {
        WYJ.Web.Utils.MessageBox.Show(this, "错误:" + ex.Message);
    }
}