首页 > 代码库 > .net 分页案例效果

.net 分页案例效果

 string path_query = "RigthAdvent.aspx?";
#region 分页

        /// <summary>
        
/// 分页代码
        
/// </summary>
        
/// <param name="totalResult">总的结果条数</param>
        
/// <param name="pageSize">每一页的大小</param>
        
/// <param name="currentPage">当前页码</param>
        
/// <returns>分页的html</returns>
        public string Paging(double totalResult, int pageSize, int currentPage)
        {
            //在形如“上一页 1 ... 4 5 6 7 8 ... 100 下一页”的分页效果中,
            
//leftNum = 4,相应的rightNum = 8; currentPage = 6; step = 2(从4到6间的差叫做step)

            string pageUrl = "";
            //左右延伸个数
            int step = 4;

            int leftNum;
            int rightNum;

            //总页数
            string allshuju = totalResult.ToString();
            int totalPageCount = (int)Math.Ceiling(totalResult / pageSize);

            //是否显示上一页
            if (currentPage > 1)
            {
                pageUrl += "<a href=http://www.mamicode.com/‘" + path_query + "page=" + (currentPage - 1) + "‘>上一页</a>";
            }

            //此时应该这样显示页码 上一页 1 ...  5 6 7 8 9 ... 100 下一页
            
//即当前浏览的页数在中间几页
            if ((currentPage - step) > 2 && (totalPageCount > step * 2 + 2))
            {

                //第一页
                pageUrl += "<a href=http://www.mamicode.com/‘" + path_query + "page=1‘>1</a>";

                //标识变量,标识当前页是否是倒数几页,该变量在下面会用到
                bool isLastFiewPages = false;


                //判断是否是倒数后几页
                if (currentPage + (step * 2 + 1 + 1) > totalPageCount)
                {
                    leftNum = totalPageCount - (step * 2 + 1);
                    rightNum = totalPageCount;
                    isLastFiewPages = true;
                }
                else
                {
                    leftNum = currentPage - step;
                    rightNum = currentPage + step;
                }


                if (leftNum - 1 > 1)
                {
                    pageUrl += "<span>...</span>";
                }

                //拼装分页代码
                for (int i = leftNum; i <= rightNum; i++)
                {
                    if (i == currentPage)
                    {
                        //当前页 
                        pageUrl = pageUrl + "<span class=‘currentpage‘>" + i + "</span>";
                    }
                    else
                    {
                        //有连接
                        pageUrl = pageUrl + "<a href=http://www.mamicode.com/‘" + path_query + "page=" + i + "‘>" + i + "</a>";
                    }
                }
                //最后一页
                if (!isLastFiewPages)
                {
                    pageUrl += "<span>...</span><a href=http://www.mamicode.com/‘" + path_query + "page=" + totalPageCount + "‘>" + totalPageCount + "</a>";
                }

            }
            //此时应该这样显示页码 上一页 1 2 3 4 5 6 ... 100 下一页或者
            
//当总页数小于或等于(2 * step + 1 + 1)的时候应该这样显示 上一页 1 2 3 4 5 下一页
            else
            {
                if (currentPage <= 0)
                {
                    currentPage = 1;
                }

                leftNum = 1;
                //rightNum总页数和step * 2 + 1 + 1中较小的那个,
                rightNum = totalPageCount < (step * 2 + 1 + 1) ? totalPageCount : (step * 2 + 2);

                for (int i = leftNum; i <= rightNum; i++)
                {
                    if (i == currentPage)
                    {
                        pageUrl += "<span class=‘currentpage‘>" + currentPage + "</span>";
                    }
                    else
                    {
                        pageUrl += "<a href=http://www.mamicode.com/‘" + path_query + "page=" + i + "‘>" + i + "</a>";
                    }
                }

                //如果总条数大于前几页连续显示的条数需要这样显示 上一页 1 2 3 4 5 6 ... 100 下一页
                if (totalPageCount > (step * 2 + 2))
                {
                    if (totalPageCount - 1 > rightNum)
                    {
                        pageUrl += "<span>...</span>";
                    }
                    pageUrl += "<a href=http://www.mamicode.com/‘" + path_query + "page=" + totalPageCount + "‘>" + totalPageCount + "</a>";
                }
            }
            int tempxia = currentPage + 1;
            int tempqian = currentPage - 1; ;
            if (tempxia >= totalPageCount)
            {
                tempxia = totalPageCount;
            }
            if (tempqian <= 0)
            {
                tempqian = 1;
            }
            pageShow = @"<a href=http://www.mamicode.com/‘" + path_query + "page=" + tempxia + "‘> <img src=http://www.mamicode.com/‘images/sdhouse_05.gif‘ title=‘下一页‘/>" + currentPage + "/" + totalPageCount + "</span><a href=http://www.mamicode.com/‘" + path_query + "page=" + tempqian + "‘><img src=http://www.mamicode.com/‘images/sdhouse_03.gif‘ title=‘上一页‘/>";

            if (currentPage < totalPageCount)
            {
                pageUrl += "<a href=http://www.mamicode.com/‘" + path_query + "page=" + (currentPage + 1) + "‘ id=‘mainnext‘>下一页</a>";
            }
            return pageUrl;
        }

        #endregion 

.pageinglist {
display
: block;
height
: 50px;
border-top
: 1px solid #dedede;
background
: #fafafa;
float
: right;
margin-right
: 15px;
padding-top
: 15px;
width
: 100%;
}
pageinglist span 
{
color
: #434343;
padding
: 6px 10px 6px 10px;
/* background-color: #ff9418; */
border-radius
: 3px;
/* border: 1px solid #ff9418; */
float
: left;
font-size
: 14px;
}
pageinglist a 
{
margin-left
: 4px;
margin-right
: 4px;
border
: 1px solid #ddd;
padding
: 6px 10px 6px 10px;
border-radius
: 3px;
float
: left;
color
: #000;
font-size
: 14px;
text-decoration
: none;

} 

.net 分页案例效果