日期:2014-05-17  浏览次数:20460 次

分页页码显示的算法
分页后,想实现只显示固定个数的页码,比如显示5个,12345,当点击4的时候,显示23456
我的循环是这么写的
很明显,是不对的
有没有人可以帮我修改一下
for (int i = (pds.CurrentPageIndex - 3) > 0 ? (pds.CurrentPageIndex  - 3) : 1; i <= ((pds.CurrentPageIndex  + 5) < pds.PageCount ? (pds.CurrentPageIndex  + 5) : pds.PageCount); i++)


pds是PagedDataSource
pds.CurrentPageIndex是当前页的索引,不是当前页的页码

------解决方案--------------------
 public static void PagsArithmetic()
        {
            //假设的页码
            int[] pags = new int[5];
            //被点击的页码
            int index = 0;
            //总页数
            int pagCount = 7;
            for (int i = 0; i < 5; i++)
            {
                //判读是否要变动页码,如果不大于3就为初始页码
                if (index > 3)
                {
                    pags[i] = index - 2 + i;
                    //超出最大页码,直接从后往前赋值
                    if (pags[i] > pagCount) {
                        int reduce = 4;
                        for (int j = 0; j < 5; j++)
                        {
                            pags[j] = pagCount - reduce;
                            reduce--;
                        }