C#一个显示分页页码类

在ASP.NET开发中,常用到显示分页页码程序,以下是本人写的一个类,还未完善,但已可使用,

在显示时当前页码会自动据中。并可自定义分类链接代码

using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Pager 的摘要说明
/// </summary>
public static class Pager
{
    public static int _ButtonCount = 11;
    public static string _NextPage = "<a href={0}>下一页</a>";
    public static string _LinkUrl = "?page={0}";
    public static string _LastPage = "<a href={0}>上一页</a>";

    /// <summary>
    /// 下一页链接
    /// </summary>
    public static string NextPage
    {
        get { return _NextPage; }
        set { _NextPage = value; }
    }

    /// <summary>
    /// 上一页链接
    /// </summary>
    public static string LastPage
    {
        get { return _LastPage; }
        set { _LastPage = value; }
    }

    /// <summary>
    /// 设置时为格式
    /// </summary>
    public static string NextPageText
    {
        get { return _NextPage; }
        set { _NextPage = value; }
    }

    /// <summary>
    /// 显示按钮总数
    /// </summary>
    public static int BottonCount
    {
        get { return _ButtonCount; }
        set { _ButtonCount = value; }
    }
    /// <summary>
    /// 返回页面的分页信息
    /// </summary>
    /// <param name="_RecordCount">记录总数</param>
    /// <param name="_PageSize">分页长度</param>
    /// <param name="_PageIndex">当前页</param>
    /// <returns></returns>
    public static string PageInfo(int _RecordCount, int _PageSize, int _PageIndex, string link)
    {
        string Firstpage = string.Format("<a href=" + link +"?p=1"+ ">首页</a>", "1");
        string pageinfo = "共有{0}页 / 当前第{1}页 " + Firstpage;
        string pagenext = " <a href=" + link +"?p="+"{0}"+ "><b>{1}</b></a> ";
        int PageCount = _RecordCount / _PageSize; // 页数合计
        PageCount = PageCount <= 0 ? 1 : PageCount;
        pageinfo = string.Format(pageinfo, PageCount.ToString(), _PageIndex.ToString());
        string LastPage = string.Format("<a href=" + link + "?p=" + PageCount + ">尾页</a>", PageCount);

        int n = _ButtonCount / 2;  //左右显示个数
        int StartPage = _PageIndex - n;
        int EndPage = _PageIndex + n;

        int lastpageindex=0;
        if (_PageIndex > 1)
            lastpageindex = _PageIndex - 1;
        else
            lastpageindex = 1;

        pageinfo += "<b> </b>" + "<a href=" + link + "?p=" + lastpageindex.ToString() + ">上一页</a>";

        int nextpageindex = 0;
        if (_PageIndex < PageCount)
            nextpageindex = _PageIndex + 1;
        else
            nextpageindex = PageCount;

        LastPage = "<a href=" + link + "?p=" + nextpageindex.ToString() + ">下一页</a>" + "<b> </b>" + LastPage;

        if (EndPage > PageCount)
        {
            StartPage = (_PageIndex - n) - (EndPage - PageCount);
            EndPage = PageCount;
        }
        if (StartPage < 1)
        {
            EndPage = _ButtonCount;
            StartPage = 1;
        }

        for (int i = StartPage; i <= EndPage; i++)
        {
            if (i != _PageIndex)
                pageinfo += string.Format(pagenext, i,i);
            else
                pageinfo += " <b>" + i.ToString() + "</b> ";
        }
        pageinfo += LastPage;
        return pageinfo;
    }
}

posted @ 2008-03-27 18:04  supers  阅读(522)  评论(0)    收藏  举报