NET分页实现及代码

8/10/2015来源:ASP.NET技巧人气:1760

NET分页实现及代码

最近在写一个关于NET的框架,写到后面果不其然的就遇到了分页,自己看了很多关于分页的并自己结合写了一个,晒出来和大家分享一下,第一次写博客望大家多多提意见啦。。。

cs文件分页代码:

1 Paging p = new Paging(3, sql语句);     //每页3条 2 ViewState["List"] = p.GetDataTable(); //查询出要显示的DataTable3 ViewState["page"] = p.GetPageing(this); //分页显示html

前台调用:

1 <%=ViewState["page"]%>

最近刚发现的ViewState这个函数,只要后台定义前台就可以调用很方便。(这个是不是自己out了,现在才发现)

下面是关于Paging类:

  1     public class Paging  2     {  3         PRivate string pagedNum;  //定义分页条,输出到前台      4         private int pageSize;     // 定义每页有多少条数据量.      5         private string url;       //获取当前页  6         private int countNum;    //总条数  7         private string SelSql;   //查询SQL语句  8         private DataSet ds;  9  10         public Paging(int pageSize, string SelSql) 11         { 12             this.pageSize = pageSize; 13             this.url = HttpContext.Current.Request.Url.LocalPath; 14             this.SelSql = SelSql; 15             GetPageDate(SelSql); 16         } 17         //定义样式a标签第一个样式     18         private readonly string CSStagA = @"<a href='{0}?page={1}' >{2}</a>";            //{0}{1}{2}代表url和参数名,参数值,页码值     19         //定义样式a标签第二个样式     20         //private readonly string csstagA1 = "<a style='font-size:13px;font-weight:bold;margin:0 4px 0 4px'>{0}</a>"; 21         private readonly string csstagA1 = "<span class='pc'>{0}</span>"; 22  23  24         public delegate int GetDelegate(); 25  26  27         /// <summary>     28         /// 生成分页条     29         /// </summary>     30         /// <param name="pageIndex">当前页</param>     31         /// <param name="del">获得数据条数的方法</param>     32         /// <returns></returns>     33         public string GetPageing(Page p) 34         { 35             string index = p.Request.QueryString["page"]; 36             int i; 37             if (!string.IsNullOrEmpty(index) && int.TryParse(index, out i)) 38             { 39                 pagedNum = GetPagegNum(i, GetPageCount(i, countNum)); 40             } 41             else 42             { 43                 pagedNum = GetPagegNum(1, GetPageCount(1, countNum)); 44             } 45              46             return pagedNum; 47         } 48  49         private int GetPageCount(int pageIndex, int countPage)     //获得总页数     50         { 51             int Count = 0; 52             Count = countPage; 53             double c = Count * 1.0 / pageSize; 54             return (int)Math.Ceiling(c); 55         } 56  57  58         private string GetPagegNum(int pageIndex, int pageCount)           //类似   上一页 1 ... 7 8 9 ⑩ 11 12 13 14 下一页   ⑩是当前选中页     59         { 60             StringBuilder sb = new StringBuilder(); 61             List<int> ns = new List<int>();              //用于接收当前页范围内的数字     62             string[] numList = new string[12];           //12个字符串数组,存放分页条数据     63             numList[0] = "";       //“上一页”位置     64             numList[11] = "";      //“下一页”位置         65             if (pageIndex > 1)                                         //判断当前页     66             { 67                 numList[0] = string.Format(csstagA, url, (pageIndex - 1), "<上一页"); 68             } 69             if (pageIndex < pageCount) 70             { 71                 numList[11] = string.Format(csstagA, url, (pageIndex + 1), "下一页>"); 72             } 73             if (pageIndex >= 10)    //当前页大于10页的状态     74             { 75                 //主要的     76                 numList[1] = string.Format(csstagA, url, 1, 1); 77                 numList[2] = "..."; 78                 //int index = 0;     79                 if (pageIndex + 4 >= pageCount)    //如果当前页加4页小于总页数     80                 { 81                     for (int i = pageCount - 7; i < pageCount + 1; i++) 82                     { 83                         //index = i;     84                         ns.Add(i); 85                     } 86                     for (int j = 0; j <= 7; j++)    //遍历ns页码值填充到分页条     87                     { 88                         if (ns[j] == pageIndex)     //判断是否为当前页码,来使用不同css样式     89                         { 90                             numList[j + 3] = string.Format(csstagA1, ns[j]);  //因为字符串数组前3位分别为“上一页”,“1”,“...”,所以从第四位填充7个     91                         } 92                         numList[j + 3] = string.Format(csstagA, url, ns[j], ns[j]); 93                     } 94                 } 95                 for (int i = pageIndex - 3; i <= pageIndex + 4; i++) 96                 { 97                     //index = i;     98                     ns.Add(i); 99                 }100                 for (int j = 0; j <= 7; j++)101                 {102                     if (ns[j] == pageIndex)103                     {104                         numList[j + 3] = string.Format(csstagA1, ns[j]);105                     }106                     else107                     {108                         numList[j + 3] = string.Format(csstagA, url, ns[j], ns[j]);109                     }110                 }111             }112             else   //10页以下的状态    113             {114                 if (pageCount >= 10)               //页数大于等于10    115                 {116                     for (int i = 1; i <= 10; i++)117                     {118                         if (i == pageIndex)119                         {120                             numList[i] = string.Format(csstagA1, i);121                         }122                         else123                         {124                             numList[i] = string.Format(csstagA, url, i, i);125                         }126                     }127                 }128                 else            //页数小于10    129                 {130                     for (int i = 1; i <= pageCount; i++)131                     {132                         if (i == pageIndex)133                         {134                             numList[i] = string.Format(csstagA1, i);135                         }136                         else137                         {138                             numList[i] = string.Format(csstagA, url, i, i);139                         }140                     }141                 }142             }143             sb.Append("<div class='page'>");144             for (int i = 0; i < numList.Length; i++)   //将字符串数组填入StringBulider中    145             {146                 sb.Append(numList[i]);147             }148 149             sb.AppendFormat(" 共{0}/{1}条", pageIndex, pageCount);150             sb.Append("</div>");151             return sb.ToString();   //返回,并在前台 <span id="pagedspan"><%=pagedNum %></span>    152         }153 154         /// <summary>155         /// 获取分页数据和总页数156         /// </summary>157         /// <param name="pageSql"></param>158         public void GetPageDate(string pageSql)159         {160             int pageIndex = Req.QueryString("page").ToInt(1);161 162             StringBuilder strSql = new StringBuilder();163             strSql.Append(@"declare @startRow int164 declare @endRow int165 declare @pageSize int166 declare @pageIndex int167 set @pageSize=@pageSize2 168 set @pageIndex=@pageIndex2 169 set @startRow=(@pageIndex-1) * @pageSize+1 170 set @endRow=@pageSize*@pageIndex");171             strSql.Append(" select * from(");172             strSql.Append("select *,ROW_NUMBER() OVER (ORDER BY getdate()) pageID from(");173             strSql.Append(pageSql);174             strSql.Append(")tab_a");175             strSql.Append(")tab_b where pageID between @startRow and @endRow");176 177