自己写的一个分页的代码(有添加样式参考)

 if (currentPageCount == 1)//总页数等于1
            {
                return;//只有一页没有必要出现页码。
            }
            int categoryId = 0;

            if (!int.TryParse(Request.QueryString["categoryId"], out categoryId))
            {
                categoryId = 0;
            }
            int start = currentPageIndex - 3;//起始位置。
            if (start < 1)
            {
                start = 1;
            }
            int end = start + 6;//终止位置.
            if (end > currentPageCount)
            {
                end = currentPageCount;
                start = currentPageCount - 6>0?currentPageCount-6:1;//该起始位置。(考虑到总页数小于9的情况)

            }
            StringBuilder sb = new StringBuilder();

         

            if (currentPageIndex == 1)
            {
                sb.Append("首页");
            }
            else
            {
                sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>首页</a>", 1, categoryId));
            }

            if (currentPageIndex == 1)
            {
                sb.Append("&lt; Prev");
            }
            else
            {
                sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>&lt; Prev</a>", currentPageIndex - 1, categoryId));
            }

            /////////
            //设计。。。
            if (currentPageIndex - 7 <-2)
            {
            
            }else{
              sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>...</a>", currentPageIndex - 7, categoryId));
            }
            /////////

            for (int i = start; i <= end; i++)
            {

   
                if (i == currentPageIndex)
                {
                    sb.Append("<a class='current' href='#'>"+i+"</a>");

                }
                else
                {
                    sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>&nbsp;{0}&nbsp;</a>", i,categoryId));
                }
            }

            /////////
            //设计。。。
            if (currentPageIndex + 4 > currentPageCount){
            
            }else{
              sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>...</a>", currentPageIndex + 7, categoryId));
            }
            /////////

            if (currentPageIndex == end)
            {
                sb.Append("Next &gt;");
            }
            else
            {
                sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>Next &gt;</a>", currentPageIndex + 1, categoryId));
            }

            if (currentPageIndex == currentPageCount)
            {
                sb.Append("尾页");
            }
            else
            {
                sb.Append(string.Format("<a href='?pageIndex={0}&categoryId={1}'>尾页</a>", currentPageCount, categoryId));
            }

          
            strHtml = sb.ToString();

        }

//分页的样式代码
.pager
{ font-size:12px; margin:25px 0; text-align:center; color:#2e6ab1; line-height:200%; overflow:hidden } .pager a { border:1px solid #9aafe5; color:#2e6ab1; margin:0 2px; padding:2px 5px; text-decoration:none } .pager span.current { background-color:#2e6ab1; border:1px solid navy; color:#fff; font-weight:bold; margin:0 2px; padding:2px 5px } .pager a.current { background-color:#2e6ab1; border:1px solid navy; color:#fff; font-weight:bold; margin:0 2px; padding:2px 5px } #pager_top .pager { margin:15px 0 }

 

posted @ 2016-05-18 13:27  灿烂1号  阅读(268)  评论(0)    收藏  举报