ASP.NET数据分页

  1      /// <summary>
  2         /// 分页
  3         /// </summary>
  4         /// <param name="PageName">页面地址</param>
  5         /// <param name="PageCount">分页总数</param>
  6         /// <param name="PageIndex">当前页</param>
  7         /// <returns></returns>
  8         public static string GetPageListHtml(string PageName, int PageCount, int PageIndex)
  9         {
 10             StringBuilder ResHtml = new StringBuilder();
 11             ResHtml.Append("<div id='pager' class='scott'>");
 12             if (PageIndex >= 4 && PageCount >= 6)
 13             {
 14                 ResHtml.Append("<a href=\"" + PageName + "&page=1\">首页</a>");
 15             }
 16             if (PageIndex >= 2)
 17             {
 18                 ResHtml.Append("<a class='toppage' href='" + PageName + "&page=" + (PageIndex - 1).ToString() + "'>&nbsp</a>");
 19             }
 20             if (PageCount <= 5)
 21             {
 22                 for (int i = 1; i <= PageCount; i++)
 23                 {
 24                     if (PageIndex == i)
 25                     {
 26                         ResHtml.Append("<span class=\"current\">" + i.ToString() + "</span>");
 27                     }
 28                     else
 29                     {
 30                         ResHtml.Append("<a href='" + PageName + "&page=" + i.ToString() + "'>" + i.ToString() + "</a>");
 31                     }
 32                 }
 33             }
 34             else
 35             {
 36                 for (int i = 1; i <= 5; i++)
 37                 {
 38                     if (PageIndex == 1 || PageIndex == 2)
 39                     {
 40                         if (PageIndex == i)
 41                         {
 42                             ResHtml.Append("<span class=\"current\">" + i.ToString() + "</span>");
 43                         }
 44                         else
 45                         {
 46                             ResHtml.Append("<a href='" + PageName + "&page=" + i.ToString() + "'>" + i.ToString() + "</a>");
 47                         }
 48                     }
 49                     else if ((PageCount - PageIndex) == 0 || (PageCount - PageIndex) == 1)
 50                     {
 51                         if ((PageCount - PageIndex) == 0 && i == 5)
 52                         {
 53                             ResHtml.Append("<span class=\"current\">" + (PageCount - 5 + i).ToString() + "</span>");
 54                         }
 55                         else if ((PageCount - PageIndex) == 1 && i == 4)
 56                         {
 57                             ResHtml.Append("<span class=\"current\">" + (PageCount - 5 + i).ToString() + "</span>");
 58                         }
 59                         else
 60                         {
 61                             ResHtml.Append("<a href='" + PageName + "&page=" + (PageCount - 5 + i).ToString() + "'>" + (PageCount - 5 + i).ToString() + "</a>");
 62                         }
 63                     }
 64                     else
 65                     {
 66                         if (i == 3)
 67                         {
 68                             ResHtml.Append("<span class=\"current\">" + (PageIndex - 3 + i).ToString() + "</span>");
 69                         }
 70                         else
 71                         {
 72                             ResHtml.Append("<a href='" + PageName + "&page=" + (PageIndex - 3 + i).ToString() + "'>" + (PageIndex - 3 + i).ToString() + "</a>");
 73                         }
 74                     }
 75                 }
 76 
 77             }
 78             if ((PageCount - PageIndex) >= 1)
 79             {
 80                 ResHtml.Append("<a class='footpage' href='" + PageName + "&page=" + (PageIndex + 1).ToString() + "'>&nbsp;</a>");
 81             }
 82             if ((PageCount - PageIndex) >= 3 && PageCount >= 6)
 83             {
 84                 ResHtml.Append("<a href='" + PageName + "&page=" + PageCount.ToString() + "'>尾页</a>");
 85             //}
 86             ResHtml.Append("</div>");
 87             return ResHtml.ToString();
 88         }
 89 
 90 
 91      /// <summary>
 92         /// 获取总页数
 93         /// </summary>
 94         /// <param name="Recordcount"></param>
 95         /// <param name="PageSize"></param>
 96         /// <returns></returns>
 97         public static int GetAllPageByRecordCount(int Recordcount, int PageSize)
 98         {
 99             int countNum;
100             if (Recordcount == 0)
101             {
102                 countNum = 0;
103             }
104             else
105             {
106                 if (Recordcount % PageSize > 0)
107                 {
108                     countNum = Convert.ToInt32(Recordcount / PageSize) + 1;
109                 }
110                 else
111                 {
112                     countNum = Convert.ToInt32(Recordcount / PageSize);
113                 }
114             }
115             return countNum;
116         }
117 
118 
119 
120      /// <summary>
121         /// 返回页面序号列表链接
122         /// </summary>
123         /// <param name="funClick">链接的目的</param>
124         /// <param name="countNum">总页数</param>
125         /// <param name="currentPage">当前页数</param>
126         /// <returns>字符串类型</returns>
127         public static string ShowPageListHtml(string funClick, int Recordcount, int PageSize, int currentPage)
128         {
129             int countNum;
130             countNum = GetAllPageByRecordCount(Recordcount, PageSize);
131             if (currentPage > countNum)
132             {
133                 currentPage = countNum;
134             }
135             if (currentPage < 1)
136             {
137                 currentPage = 1;
138             }
139             return GetPageListHtml(funClick, countNum, currentPage);
140         }

 

posted @ 2016-08-26 15:40  zhang_r  阅读(382)  评论(0编辑  收藏  举报