小型数据分页

 

//分页类

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;
using System.Text;
/// <summary>
/// getPage 的摘要说明
/// </summary>
public class getPage
{
    public getPage()
    {

    }
    public static void smallbigpage(int pagecount, int pageindex, int pagesize, out int smallpage, out int bigpage, out int count)
    {
        if ((pagecount % pagesize) == 0)
        {
            count = pagecount / pagesize;
        }
        else
        {
            string doub = Convert.ToString(pagecount / pagesize);
            count = Convert.ToInt32(doub.Split('.')[0].ToString()) + 1;
            //count =Convert.ToInt32(doub);
        }
        if (pageindex < 1)
        {
            pageindex = 1;
        }
        if (pageindex > count)
        {
            pageindex = count;
        }

        if (((pageindex) * pagesize) > pagecount)
        {
            bigpage = pagecount;
        }
        else
        {
            bigpage = ((pageindex) * pagesize);
        }
        smallpage = ((pageindex - 1) * pagesize);
    }
    public static string getpagesize(int pageindex,int pagecount,string url,int pagetype)
    {
        StringBuilder pageinfo = new StringBuilder();
        pageinfo.Append("共 <font color=red>" + pagecount + "</font> 页&nbsp;&nbsp;&nbsp;");
        switch (pagetype)
        {
               
            case 1:
                if (pageindex == 1)
                {
                   
                    pageinfo.Append("首页 上一页");
                }
                else
                {
                    pageinfo.Append("<a href=\"?pageindex=1&" + url + "\">首页</a> &nbsp;");
                    pageinfo.Append("<a href=\"?pageindex=" + (pageindex - 1) + "&" + url + "\">上一页 </a>&nbsp;");
                }
                for (int i = pageindex - 4; i <= pageindex - 1; i++)
                {
                    if (i > 0)
                    {
                        pageinfo.Append("<a href=\"?pageindex=" + i + "&" + url + "\">[" + i + "]</a> &nbsp;");
                    }
                }
                for (int i = pageindex; i < pageindex + 4; i++)
                {
                    if (i <= pagecount)
                    {
                        pageinfo.Append("<a href=\"?pageindex=" + i + "&" + url + "\">[" + i + "]</a> &nbsp;");
                    }
                }
                if (pageindex < pagecount)
                {
                    pageinfo.Append("<a href=\"?pageindex=" + (pageindex + 1) + "&" + url + "\">下一页</a>&nbsp; ");
                }
                pageinfo.Append("<a href=\"?pageindex=" + pagecount + "&" + url + "\">尾页</a>&nbsp;");
                break;
            default:
                break;
        }

        return pageinfo.ToString();
       
    }
}

 

//掉用页面

//声明

protected DataTable ds = new DataTable();
    protected string where;
    protected int smallpage;
    protected int bigpage;
    protected int pagesize = 20;
    protected int pageindex = 1;
    protected int count;
    protected string url;

//判断

if (Request["pageindex"] != null)
        {
            pageindex = Convert.ToInt32(Request["pageindex"].ToString());
        }
        ds = DBHeleper.GetDataSet("获取数据返回DataTable类型 ");

//分页
        getPage.smallbigpage(ds.Rows.Count, pageindex, pagesize, out smallpage, out bigpage, out count);

循环读取数据

if (smallpage >= 0)
{
            for (int i = smallpage; i < bigpage; i++)
            {
                DataRow rs = ds.Rows[i];

               //显示数据

rs[0].ToString();

            }
}

//分页 返回string类型

 getPage.getpagesize(pageindex, count, url, 1)

 

你可能感兴趣的:(分页)