分页

 public string GetPageNum(int curpage, int pagesize, int totalSize)
        {

            int allpage = pagesize;
            int next = 1;
            int pre = 1;
            int startcount = 0;
            int endcount = 0;

            pagestr = "";

            if (curpage < 1) { curpage = 1; }


            if (curpage >= 1)
            {
                next = curpage + 1;

            }

            if (curpage > 1)
            {
                pre = curpage - 1;
            }


            startcount = (curpage + 5) > allpage ? allpage - 9 : curpage - 4;//中间页起始序号
            //中间页终止序号
            endcount = curpage < 5 ? 10 : curpage + 5;
            if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
            if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
            pagestr = "每页" + size.ToString() + "项" + "&nbsp;&nbsp;";

            pagestr += curpage > 1 ? "<a href=\"" + pathUrls + "1/\">首页</a>&nbsp;&nbsp;<a href=\"" + pathUrls + pre + "/\">上一页</a>" : "首页 上一页";
            //中间页处理,这个增加时间复杂度,减小空间复杂度
            for (int i = startcount; i <= endcount; i++)
            {
                pagestr += curpage == i ? "&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>" : "&nbsp;&nbsp;<a href=\"" + pathUrls + i + "/\">" + i + "</a>";
            }
            pagestr += curpage != allpage ? "&nbsp;&nbsp;<a href=\"" + pathUrls + next + "/\">下一页</a>&nbsp;&nbsp;<a href=\"" + pathUrls + allpage + "/\">末页</a>" : " 下一页 末页";


            pagestr += "&nbsp;&nbsp;当前:" + curpage.ToString() + "/" + allpage.ToString() + "页";
            return pagestr;

        }

posted @ 2010-12-09 16:53  tangself  阅读(111)  评论(0编辑  收藏  举报