ASP.NET分页技术

来源:互联网 发布:黑色沙漠城镇优化 编辑:程序博客网 时间:2024/05/16 19:46

自己在做网站时经常使用的一个分页程序,希望大家指点!

 

public static StringBuilder PageBtn(int currentPage, int numInOneLine, string url, PagedDataSource pds)//返回索引字符串,numInOneLine为每行索引的个数,currentPage为当前页;
    {
            StringBuilder sb = new StringBuilder();
            int currentLine; //当前索引行

            if (currentPage % numInOneLine != 0)//判断当前页是否为每行索引的个数的整数倍;
            {
                currentLine= currentPage / numInOneLine + 1;
            }
            else
            {
                currentLine=currentPage / numInOneLine;
            }
            //添加上一索引行连接
            if (currentLine == 1)
            {
                sb.Append("<<&nbsp");
            }
            else
            {
                sb.Append("<a href='");
                sb.Append(url);
                sb.Append("?currentPage=");
                sb.Append(Convert.ToString((currentLine-1)*numInOneLine));
                sb.Append("'><<</a>&nbsp");
            }

            //添加页码索引
            for (int i = 1; i <= numInOneLine; i++)
            {
                //<a href="Datalist.aspx" style="color:Red">[]</a>
                if ((currentLine - 1) * numInOneLine + i<=pds.PageCount)//判断页码是否出界;
                {
                    if ((currentLine - 1) * numInOneLine + i == currentPage)//判断索引页码是否为当前页
                    {
                        sb.Append("&nbsp<font style='color:Red; text-decoration: none;'>");
                        sb.Append(currentPage.ToString());
                        sb.Append("</font>&nbsp");
                    }
                    else
                    {

                        sb.Append("<a href='");
                        sb.Append(url);
                        sb.Append("?currentPage=");
                        sb.Append(Convert.ToString((currentLine - 1) * numInOneLine + i));
                        sb.Append("'>[");
                        sb.Append(Convert.ToString((currentLine - 1) * numInOneLine + i));
                        sb.Append("]</a>&nbsp");
                    }
                }
            }

            int totalLine;//定义总索引行数
            if (pds.PageCount % numInOneLine != 0)//判断总页数是否为每行索引的个数的整数倍;
            {
                totalLine = pds.PageCount / numInOneLine + 1;
            }
            else
            {
                totalLine = pds.PageCount / numInOneLine;
            }
            //添加下一索引行
            if (currentLine == totalLine)
            {
                sb.Append(">>");
            }
            else
            {
                sb.Append("<a href='");
                sb.Append(url);
                sb.Append("?currentPage=");
                sb.Append(Convert.ToString(currentLine * numInOneLine + 1));
                sb.Append("'>>></a>");
            }
       
        return sb;

    }

原创粉丝点击