C#分页

一个简单的C#分页小模块

using System;
namespace Page
{
    class Program
    {
        //url连接,countdatas表数据总行数,currentpage第几页(0是第一页),everpage每页多少行,beforlaterln前后长度
        public String pagestr(String url, int countdatas, int currentpage, int everpage, int beforlaterln)
        {
            String page = "";
            int intpart = countdatas / everpage;//整数
            int remainderpart = countdatas % everpage;//余数
            int countpage = intpart;//取整数
            currentpage = currentpage < 0 ? 0 : currentpage;
            if (remainderpart == 0)
            {//余数判断
                currentpage = currentpage > countpage - 1 ? countpage - 1 : currentpage;
                int pp = currentpage + 1;
                String strbe = "";
                int pagebefore = pp - beforlaterln - 1;
                for (int i = pp; i > pagebefore; i--)
                {
                    if (i > 0 && i < pp)
                    {
                        strbe = "" + i + "" + strbe;
                    }
                }
                if (pp <= 1) { strbe = ""; } else { strbe = "首页" + "上一页" + strbe; }
                String strmid = "" + pp + "";
                String strlater = "";
                int pagelater = pp + beforlaterln;
                for (int i = pp; i < pagelater; i++)
                {
                    if (i < countpage)
                    {
                        strlater += "" + (i + 1) + "";
                    }
                }
                if (pp >= countpage) { } else { strlater += "下一页" + "尾页"; }
                page = strbe + strmid + strlater;
            }
            else
            {
                countpage += 1;//总页数
                currentpage = currentpage > countpage - 1 ? countpage - 1 : currentpage;
                int pp = currentpage + 1;
                String strbe = "";
                int pagebefore = pp - beforlaterln - 1;
                for (int i = pp; i > pagebefore; i--)
                {
                    if (i > 0 && i < pp)
                    {
                        strbe = "" + i + "" + strbe;
                    }
                }
                if (pp <= 1) { strbe = ""; } else { strbe = "首页" + "上一页" + strbe; }
                String strmid = "" + pp + "";
                String strlater = "";
                int pagelater = pp + beforlaterln;
                for (int i = pp; i < pagelater; i++)
                {
                    if (i < countpage)
                    {
                        strlater += "" + (i + 1) + "";
                    }
                }
                if (pp >= countpage) { } else { strlater += "下一页" + "尾页"; }
                page = strbe + strmid + strlater;
            }
            return "共" + countpage + "页" + page;
        }
        static void Main(string[] args)
        {
            Program pg = new Program();
            String str = pg.pagestr("http://www.abc.com?", 195, 11, 10, 8);
            Console.WriteLine(str);
        }
    }
}

运行后得到一下字符串

共20页首页
上一页
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
下一页
尾页

希望对大家有帮助,谢谢!

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