Net通用分页(可以选择页码的显示,且有中英选择)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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>
/// 启程 通用分页类 www.letwego.cn
/// </summary>
public class Pageing
{
    #region 成员

    /// <summary>
    /// 数据源 Table
    /// </summary>
    public DataTable dtblTableww = new DataTable();

    /// <summary>
    /// 每页记录数
    /// </summary>
    public int intPageSizeww = 20;

    /// <summary>
    /// 需要获取第几页的数据,从 1 开始
    /// </summary>
    public int intPageNowww = 1;

    /// <summary>
    /// 首页 显示样式
    /// </summary>
    public string strFirstww = "";

    /// <summary>
    /// 上一页 显示样式
    /// </summary>
    public string strPrevww = "";

    /// <summary>
    /// 下一页 显示样式
    /// </summary>
    public string strNextww = "";

    /// <summary>
    /// 尾页 显示样式
    /// </summary>
    public string strLastww = "";

    /// <summary>
    /// 跳转 的url链接
    /// </summary>
    public string strTurnUrlww = "";

    /// <summary>
    /// 跳转的url链接的参数前面不要加?或&
    /// </summary>
    public string strOptionww = "";


    private string strCountww = "";   //共N条信息
    private string strPageww = "";    //第N页/共N页    
    private string strTurnww;   //跳转控件

    #endregion

    #region 属性

    private int intRsCounww = 0;    //记录总数

    /// <summary>
    /// 总记录数
    /// </summary>
    public int GetRsCount
    {
        get
        {
            return this.intRsCounww;
        }
    }

    private int intPageCountww = 0; //总页数

    /// <summary>
    /// 总页数
    /// </summary>
    public int GetPageCount
    {
        get
        {
            return this.intPageCountww;
        }
    }

    private string strPageShowww = ""; //显示页码的字符串
    /// <summary>
    /// 显示页码的字符串
    /// </summary>
    public string GetPageShow
    {
        get
        {
            return this.strPageShowww;
        }
    }

    #endregion

    #region 初始构造
    /// <summary>
    /// 初始构造
    /// </summary>
    public Pageing()
    {
        //首页
        strFirstww = "";
        //上一页              
        strPrevww = "";
        //下一页               
        strNextww = "";
        //尾页               
        strLastww = "";
        //跳转控件                  
        strTurnww = "";
        //显示页码的字符串
        strPageShowww = "";
        //当前页
        intPageNowww = GetNumeric(GetRequest("Page"));

        if (intPageNowww == 0)
        {
            intPageNowww = 1;
        }
    }
    #endregion

    #region 处理分页及显示页码
    /// <summary>
    /// 分页,返回分页后的数据 Table
    /// </summary>
    /// <param name="bolCount">是否显示 共N条信息</param>
    /// <param name="bolPage">是否显示 第N页/共N页</param>
    /// <param name="bolFirst">是否显示 首页</param>
    /// <param name="bolLast">是否显示 尾页</param>
    /// <param name="bolTurn">是否显示 跳转控件</param>
    /// <param name="IsEnglish">是否 用中文显示</param>
    /// <returns>返回分页后的数据 Table</returns>
    public DataTable BeginPage(bool bolCount, bool bolPage, bool bolFirst, bool bolLast, bool bolTurn, bool IsChinese)
    {

        //记录总数
        intRsCounww = dtblTableww.Rows.Count;

        //总页数
        intPageCountww = (intRsCounww + intPageSizeww - 1) / intPageSizeww;

        //超出最大页码
        if (intPageNowww > intPageCountww)
        {
            intPageNowww = intPageCountww;
        }

        //中文分页
        if (IsChinese)
        {
            //跳转
            strTurnww = "<input value='" + intPageNowww.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:25px;'><input name='btnGo' type='button' id='btnGo' value='跳转' onclick=""javascript:window.location.href='" + strTurnUrlww + "?page=' + document.getElementById('txtPageGo').value + '" + "&" + strOptionww + "'"">";
            //共N条信息
            strCountww = "共" + intRsCounww.ToString() + "条信息";
            //第N页/共N页
            strPageww = "第" + intPageNowww.ToString() + "页/共" + intPageCountww.ToString() + "页";

            if (strFirstww == "")
            {
                strFirstww = "首页";
                strPrevww = "上一页";
                strNextww = "下一页";
                strLastww = "尾页";
            }
        }
        else//英文文分页
        {
            //跳转
            strTurnww = "<input value='" + intPageNowww.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:25px;'><input name='btnGo' type='button' id='btnGo' value='Goto' onclick=""javascript:window.location.href='" + strTurnUrlww + "?page=' + document.getElementById('txtPageGo').value + '" + "&" + strOptionww + "'"">";
            //共N条信息
            strCountww = "Total " + intRsCounww.ToString() + " Infos";
            //第N页/共N页
            strPageww = " " + intPageNowww.ToString() + "/" + intPageCountww.ToString() + " ";

            if (strFirstww == "")
            {
                strFirstww = " First ";
                strPrevww = " Previous ";
                strNextww = " Next ";
                strLastww = " Last ";
            }
        }


        DataTable MyTable;

        int intGetFirstRs = 0;//获取本页数据的开始下标
        int intGetLastRs = 0;//获取本页数据的结束下标

        string strFirstww2;
        string strPrevww2;
        string strNextww2;
        string strLastww2;


        //没有记录
        if (intRsCounww <= 0)
        {
            strPageShowww = strCountww;
            MyTable = dtblTableww;
        }
        //有记录
        else
        {
            //只有一页
            if (intPageCountww <= 1)
            {
                strPageShowww = strCountww + "&nbsp;&nbsp;" + strPageww;
                MyTable = dtblTableww;
            }
            //不止一页
            else
            {
                //第一页
                if (intPageNowww == 1)
                {
                    strFirstww2 = strFirstww;
                    strPrevww2 = strPrevww;
                }
                else
                {
                    strFirstww2 = "<a href=""" + strTurnUrlww + "?page=1" + "&" + strOptionww + """>" + strFirstww + "</a>";
                    strPrevww2 = "<a href=""" + strTurnUrlww + "?page=" + Convert.ToString(intPageNowww - 1) + "&" + strOptionww + """>" + strPrevww + "</a>";
                }

                //最后一页
                if (intPageNowww == intPageCountww)
                {
                    strNextww2 = strNextww;
                    strLastww2 = strLastww;
                }
                else
                {
                    strNextww2 = "<a href=""" + strTurnUrlww + "?page=" + Convert.ToString(intPageNowww + 1) + "&" + strOptionww + """>" + strNextww + "</a>";
                    strLastww2 = "<a href=""" + strTurnUrlww + "?page=" + intPageCountww + "&" + strOptionww + """>" + strLastww + "</a>";
                }

                //----处理显示页码-----------
                if (bolCount == true)//共N条信息
                {
                    strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strCountww;
                }
                if (bolPage == true)//第N页/共N页
                {
                    strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strPageww;
                }
                if (bolFirst == true) //首页
                {
                    strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strFirstww2;
                }
                strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strPrevww2;//上一页
                strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strNextww2;//下一页
                if (bolLast == true)//尾页
                {
                    strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strLastww2;
                }
                if (bolTurn == true)//跳转控件
                {
                    strPageShowww = strPageShowww + "&nbsp;&nbsp;" + strTurnww;
                }


                //----处理返回数据-----------
                intGetFirstRs = (intPageNowww - 1) * intPageSizeww;
                intGetLastRs = intPageNowww * intPageSizeww - 1;

                //获取结束数据下标大于总记录数时
                if (intGetLastRs >= intRsCounww)
                {
                    intGetLastRs = intRsCounww - 1;
                }

                //循环获取数据
                MyTable = dtblTableww.Clone();
                for (int I = intGetFirstRs; I <= intGetLastRs; I++)
                {
                    DataRow MyRow = dtblTableww.Rows[I];
                    MyTable.ImportRow(MyRow);
                }
            }
        }
        return MyTable;
    }
    #endregion


    #region 将字符串转换成整数
    /// <summary>
    /// 将字符串转换成整数
    /// </summary>
    /// <param name="strValue">将字符传转换成整数</param>
    /// <returns>int</returns>
    private int GetNumeric(string strValue)
    {

        int IReturn;

        if (strValue == null)
        {
            IReturn = 0;
            return IReturn;
        }
        try
        {
            IReturn = Convert.ToInt32(strValue);
        }
        catch
        {
            IReturn = 0;
        }
        return IReturn;
    }
    #endregion

    #region 获取Request值
    /// <summary>
    /// 获取Request值
    /// </summary>
    /// <param name="strRequestName">Request名称</param>
    /// <returns>null则为空</returns>
    private string GetRequest(string strRequestName)
    {

        if (HttpContext.Current.Request["" + strRequestName + ""] != null)
        {
            return HttpContext.Current.Request["" + strRequestName + ""];
        }
        else
        {
            return "";
        }
    }
    #endregion


    #region --------------------------------调用实例--------------------------
    ///// <summary>
    ///// 调用实例
    ///// </summary>
    //protected void BindData()
    //{
    //    Student MyStudent = new Student();

    //    DataTable MyDataTable = MyStudent.GetList("LIST", null);

    //    Pageing MyPageing = new Pageing();
    //    MyPageing.dtblTableww = MyDataTable;//数据源
    //    MyPageing.intPageSizeww = 4;//每页显示多少条
    //    MyPageing.strTurnUrlww = "MyRepeater.aspx";//跳转页面
    //    MyPageing.strOptionww = "DoWhat=List";//跳转页面带的所有参数

    //    //获取当前页数据
    //    this.DataList1.DataSource = MyPageing.BeginPage(true, true, true, true, true, false);
    //    //获取当前页分页显示
    //    this.Label1.Text = MyPageing.GetPageShow;

    //    this.DataList1.DataBind();
    //}
    #endregion

}

你可能感兴趣的:(net)