模仿google分页代码

 

/// <summary>
    /// 逻辑生成上一页下一页代码
    /// 作者:沈顺和
    /// </summary>
    /// <param name="pageCounts">总数据条数</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageNum">总页码</param>
    /// <param name="request">URL传值(不要page)</param>
    public static string PageNumber(int pageCounts, int pageIndex, int pageNum, string request)
    {
        //得到当前页地址
        StringBuilder pageUp = new StringBuilder();
        string url = HttpContext.Current.Request.Url.PathAndQuery.ToString();
        int index = url.IndexOf("?");        //用于剔除Request[]
        if (index > 0)
        {
            url = url.Substring(0, index);
        }

        if (pageCounts != 0)
        {
            pageUp.Append("共有<font color=red>").Append(pageCounts.ToString()).Append("</font>条数据&nbsp;&nbsp;当前页数:<font color=red>")
                .Append(pageIndex.ToString()).Append("</font>&nbsp;&nbsp;总页数:<font id='page' name='page' color=red>").Append(pageNum.ToString())
                .Append("</font>&nbsp;&nbsp;<a href='").Append(url).Append("?page=1").Append(request)
                .Append("'>首页</a>&nbsp;&nbsp;<a href='").Append(url).Append("?page=").Append((pageIndex - 1).ToString()).Append(request)
                .Append("'>上一页</a>&nbsp;&nbsp;<a href='").Append(url).Append("?page=").Append((pageIndex + 1).ToString()).Append(request)
                .Append("'>下一页</a>&nbsp;&nbsp;<a href='").Append(url).Append("?page=").Append(pageNum.ToString()).Append(request)
                .Append("'>尾页</a>");

            if (pageNum == 1)                         //只有一页的时候
            {
                pageUp = pageUp.Remove(0, pageUp.Length);
                pageUp.Append("共有<font color=red>").Append(pageCounts.ToString()).Append("</font>条数据&nbsp;&nbsp;当前页数:<font color=red>")
                    .Append(pageIndex.ToString()).Append("</font>&nbsp;&nbsp;总页数:<font id='page' name='page' color=red>").Append(pageNum.ToString())
                    .Append("</font>&nbsp;&nbsp;首页&nbsp;&nbsp;上一页&nbsp;&nbsp;下一页&nbsp;&nbsp;尾页");
            }
            else
            {

                if (pageIndex == 1)            //是首页并且总页数不为1
                {
                    pageUp = pageUp.Remove(0, pageUp.Length);
                    pageUp.Append("共有<font color=red>").Append(pageCounts.ToString()).Append("</font>条数据&nbsp;&nbsp;当前页数:<font color=red>")
                        .Append(pageIndex.ToString()).Append("</font>&nbsp;&nbsp;总页数:<font id='page' name='page' color=red>").Append(pageNum.ToString())
                        .Append("</font>&nbsp;&nbsp;首页&nbsp;&nbsp;上一页&nbsp;&nbsp;<a href='").Append(url).Append("?page=").Append((pageIndex + 1).ToString()).Append(request)
                        .Append("'>下一页</a>&nbsp;&nbsp;<a href='").Append(url).Append("?page=").Append(pageNum.ToString()).Append(request)
                        .Append("'>尾页</a>");
                }
                if (pageNum == pageIndex)    //是尾页并且总页数不为1
                {
                    pageUp = pageUp.Remove(0, pageUp.Length);
                    pageUp.Append("共有<font color=red>").Append(pageCounts.ToString()).Append("</font>条数据&nbsp;&nbsp;当前页数:<font color=red>")
                        .Append(pageIndex.ToString()).Append("</font>&nbsp;&nbsp;总页数:<font id='page' name='page' color=red>").Append(pageNum.ToString())
                        .Append("</font>&nbsp;&nbsp;<a href='").Append(url).Append("?page=1").Append(request).Append("'>首页</a>&nbsp;&nbsp;<a href='")
                        .Append(url).Append("?page=").Append((pageIndex - 1).ToString()).Append(request).Append("'>上一页</a>&nbsp;&nbsp;下一页&nbsp;&nbsp;尾页");
                }
            }
        }
        return pageUp.ToString();
    }


posted @ 2008-12-03 15:31  hibernate3例子  阅读(81)  评论(0编辑  收藏  举报