小分页

 /// <summary>
    
/// 根据当前页码和总页数显示分页链接
    
/// </summary>
    
/// <param name="page"></param>
    
/// <param name="total"></param>
    void ShowLink(int page, int total, int bookid, int chapterid, CWml wml)
    {
        
//显示当前页的前后多少页
        const int count = 3;
        
//显示上一页
        if (page > 1)
        {
            wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + (page - 1), "上一页");
            wml.Write(
" ");
            wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + 11.ToString());
            wml.Write(
" ");
            
//如果第一页跟当前页的最前面指定页数隔了3页以上则显示省略号
            if (page - count - 1 > 1)
            {
                wml.Write(
"...");
                wml.Write(
" ");
            }
        }
        
//算出后面要显示多少页数
        int beforeCount = page - count;
        
//如果当前页后面的页数不足指定页数,则后面的页多显示几页
        if (total - page < count)
        {
            beforeCount 
-= (count - (total - page));
        }
        
//显示当前页前面的页
        for (int i = beforeCount; i < page; i++)
        {
            
if (i > 1)
            {
                wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + i, i.ToString());
                wml.Write(
" ");
            }
        }
        
//显示当前页
        wml.Write(page.ToString());
        wml.Write(
" ");
        
//算出后面要显示多少页数
        int behindCount = page + count;
        
//如果当前页前面的页数不足指定页数,则后面的页多显示几页
        if (page - count < 1)
        {
            behindCount 
+= count - page + 1;
        }
        
//显示当前页后面的页
        for (int i = page + 1; i <= behindCount; i++)
        {
            
if (i < total)
            {
                wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + i, i.ToString());
                wml.Write(
" ");
            }
        }
        
//显示最后一页和下一页
        if (page != total)
        {
            
//如果当前页跟最后一页隔了3页以上则显示省略号
            if (total - behindCount > 1)
            {
                wml.Write(
"...");
                wml.Write(
" ");
            }
            wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + total, total.ToString());
            wml.Write(
" ");
            wml.Href(
"detail.aspx?bookid=" + bookid + "&chapterid=" + chapterid + "&p=" + (page + 1), "下一页");
        }
    }
原文地址:https://www.cnblogs.com/mxw09/p/2039227.html