ASP.NET 分页技术

 以下是我自己编写的asp.net分页的一个方法,希望能给大家多多指点。
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;

    }

posted on 2008-03-05 14:09  张皓  阅读(687)  评论(0编辑  收藏  举报

导航