欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 分页代码控制生成分页html代码

程序员文章站 2022-04-06 20:17:40
在实际的开发过程中,网页可能需要用到分页实现,以下代码用c#实现前台页面的分页脚本,需要的开发人员可参考,希望有所帮助。...

在实际的开发中,网页列表中需要用到分页,以下代码用c#实现的前台页面的分页脚本,可以参考学习下。


 protected static string CreatePage(string urlFormat, int currentPage, int pageSize, int recordCount, out int totalPage)
        {
            string prevPage = string.Empty;
            string nextPage = string.Empty;
            string hostPageUri = urlFormat;
            StringBuilder sb = new StringBuilder(""); 

            totalPage = recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;

            if (totalPage <= 1)
                return string.Empty;
            if (totalPage > 100)
                totalPage = 100;

            if (recordCount != 0)
            {
                sb.Append("<div class='page'>");

                if (currentPage < 1)
                {
                    currentPage = 1;
                }
                if (currentPage > totalPage)
                {
                    currentPage = totalPage;
                }

                int startCount = (currentPage + 3) > totalPage ? totalPage - 5 : currentPage - 2;
                int endCount = currentPage < 3 ? 6 : currentPage + 3;

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

                if (endCount > totalPage)
                {
                    endCount = totalPage;
                }

                //首页
                if (endCount > 6)
                {
                    sb.AppendFormat("<a href=\"{0}\">首页</a>", hostPageUri + "p1/");
                }

                //上一页
                if (currentPage > 1)
                {
                    if (currentPage == 2)
                    {
                        sb.AppendFormat("<a href=\"{0}\">上一页</a>", hostPageUri + "p1/");
                    }
                    else
                    {
                        sb.AppendFormat("<a href=\"{0}\">上一页</a>", hostPageUri + "p" + (currentPage - 1).ToString() + "/");
                    }
                    prevPage = hostPageUri + "p" + (currentPage - 1).ToString() + "/";
                }

                //中间数字页码
                for (int i = startCount; i <= endCount; i++)
                {
                    if (i == currentPage)
                    {
                        sb.AppendFormat("<a href='javascript:;' class='active'>{0}</a>", i);
                        currentPage = i;
                    }
                    else
                    {
                        if (i > 5 && i == endCount && endCount < totalPage)
                        {
                            sb.Append("<span>...</span>");
                            sb.AppendFormat("<a href=\"{0}\" >{1}</a>", hostPageUri + "p" + totalPage.ToString() + "/", totalPage);
                        }
                        else
                        {
                            if (i == 1)
                            {
                                sb.AppendFormat("<a href=\"{0}\" >1</a>", hostPageUri + "p1/");
                            }
                            else
                            {
                                sb.AppendFormat("<a href=\"{0}\" >{1}</a>", hostPageUri + "p" + i.ToString() + "/" , i);
                            }
                        }
                    }
                }

                //下一页
                if (currentPage < totalPage)
                {
                    sb.AppendFormat("<a href=\"{0}\">下一页</a>", hostPageUri + "p" + (currentPage + 1) + "/");
                    nextPage = hostPageUri + "p" + (currentPage + 1).ToString() + "/";
                }

                //末页
                if (endCount < totalPage && totalPage > 1)
                {
                    sb.AppendFormat("<a href=\"{0}\">末页</a>", hostPageUri + "p" + totalPage.ToString() + "/");
                }

                //sb.AppendFormat("<span class=\"totalPage\">共{0}页,到第<input type=\"text\" name=\"PageIndex\" id=\"PageIndex\" class=\"pageInput\"/>页</span>", totalPage);
                sb.Append("<span class=\"totalPage\"> 到第<input type=\"text\" name=\"PageIndex\" id=\"PageIndex\" class=\"pageInput\"/>页</span>");
                sb.AppendFormat("<a href=\"javascript:;\" class=\"btn\" onclick=\"GoPage('{0}');\">GO</a>", hostPageUri);

                sb.Append("</div>");
            } 
            return sb.ToString(); 
        }


实现的效果如图所示 c# 分页代码控制生成分页html代码