发个自己写的 廖勇军Socansoft代码生成器分页重写类
在model项目下直接添加RewritePageList.cs类文件
View Code
1 using System.Collections.Generic; 2 3 /// <summary> 4 /// [重写]分页数据类 5 /// </summary> 6 /// <typeparam name="TModel">结果集中实体的类型</typeparam> 7 8 public class RewritePageList<TModel> where TModel : class 9 { 10 public RewritePageList() 11 { 12 _rewritePageInfo = new RewritePageInfo(); 13 } 14 15 public RewritePageList(RewritePageInfo rewritePageInfo) 16 { 17 _rewritePageInfo = rewritePageInfo; 18 } 19 20 public RewritePageList(int pageSize, int pageIndex, string fldSort, string strSort, string condition) 21 { 22 _rewritePageInfo = new RewritePageInfo(pageSize, pageIndex, fldSort, strSort, condition); 23 } 24 25 private List<TModel> _list; 26 private RewritePageInfo _rewritePageInfo; 27 28 /// <summary> 29 /// 分页结果集 30 /// </summary> 31 public List<TModel> List 32 { 33 get { return _list; } 34 set { _list = value; } 35 } 36 37 /// <summary> 38 /// 分页信息 39 /// </summary> 40 public RewritePageInfo RewritePageInfo 41 { 42 get { return _rewritePageInfo; } 43 set { _rewritePageInfo = value; } 44 } 45 } 46 47 /// <summary> 48 /// 分页信息 49 /// </summary> 50 public class RewritePageInfo 51 { 52 public RewritePageInfo() 53 { } 54 55 public RewritePageInfo(int pageSize, int pageIndex, string fldSort, string strSort, string condition) 56 { 57 _pageSize = pageSize; 58 _pageIndex = pageIndex; 59 _fldSort = fldSort; 60 _strSort = strSort; 61 _condition = condition; 62 } 63 64 private int _recordCount; 65 private int _pageSize; 66 private int _pageIndex; 67 private int _pageCount; 68 private int _firstIndex; 69 70 private string _fldSort; 71 private string _strSort; 72 private string _condition; 73 74 75 /// <summary> 76 /// 总记录数 77 /// </summary> 78 public int RecordCount 79 { 80 get { return _recordCount; } 81 set { _recordCount = value; } 82 } 83 84 /// <summary> 85 /// 每页大小 86 /// </summary> 87 public int PageSize 88 { 89 get { return _pageSize; } 90 set { _pageSize = value; } 91 } 92 93 /// <summary> 94 /// 页索引 95 /// </summary> 96 public int PageIndex 97 { 98 get { return _pageIndex; } 99 set { _pageIndex = value; } 100 } 101 102 /// <summary> 103 /// 总页数 104 /// </summary> 105 public int PageCount 106 { 107 get { return _pageCount; } 108 set { _pageCount = value; } 109 } 110 111 /// <summary> 112 /// 该页在所有结果中的起始索引。比如每页10条,第1页起始索引为0,第2页起始索引为10,以此类推 113 /// </summary> 114 public int FirstIndex 115 { 116 get { return _firstIndex; } 117 set { _firstIndex = value; } 118 } 119 120 /// <summary> 121 /// 排序字段 122 /// </summary> 123 public string fldSort 124 { 125 get { return _fldSort; } 126 set { _fldSort = value; } 127 } 128 129 /// <summary> 130 /// 排序方向 131 /// </summary> 132 public string strSort 133 { 134 get { return _strSort; } 135 set { _strSort = value; } 136 } 137 138 /// <summary> 139 /// 筛选条件字符 140 /// </summary> 141 public string condition 142 { 143 get { return _condition; } 144 set { _condition = value; } 145 } 146 147 /// <summary> 148 /// 开始计算页数及第一条记录的索引。 149 /// 请在赋值PageSize,PageIndex,RecoundCount完毕后调用此方法。 150 /// </summary> 151 public void Compute() 152 { 153 //计算页数 154 if (RecordCount % PageSize == 0) 155 PageCount = RecordCount / PageSize; 156 else 157 PageCount = RecordCount / PageSize + 1; 158 159 //检查页索引与页大小是否正确,并自动修正 160 if (PageIndex < 1) 161 PageIndex = 1; 162 else if (PageCount >= 1 && PageIndex > PageCount) 163 PageIndex = PageCount; 164 165 //计算第一条记录和最后一条记录的索引 166 _firstIndex = PageSize * (PageIndex - 1); 167 } 168 }
在SqlServerDAL项目下的表应用到的,直接在editable文件夹下的类文件添加以下代码进行调用
View Code
1 #region 自定义分页调用 2 /// <summary> 3 /// 获取数量[重写] 4 /// </summary> 5 private int GetCount(string _strcontidion) 6 { 7 object obj = dbHelper.ExecuteScalar(CommandType.Text, "SELECT count(*) FROM Announced where " + _strcontidion, null); 8 return int.Parse(obj.ToString()); 9 } 10 11 /// <summary> 12 /// 分页获取泛型数据列表[重写] 13 /// </summary> 14 public RewritePageList<Model.Announced> GetRPageList(RewritePageInfo Rpi) 15 { 16 Rpi.RecordCount = GetCount(Rpi.condition); 17 Rpi.Compute(); 18 19 RewritePageList<Model.Announced> pl = new RewritePageList<Model.Announced>(Rpi); 20 21 using (DbDataReader dr = dbHelper.GetPageList("Announced", Rpi.fldSort, " " + Rpi.strSort, Rpi.condition, Rpi.PageIndex, Rpi.PageSize)) 22 { 23 pl.List = GetPageList(dr, 0, Rpi.PageSize); 24 } 25 return pl; 26 } 27 #endregion
在IDAL项目添加调用接口
View Code
1 /// <summary> 2 /// 分页获取泛型数据列表[重写] 3 /// </summary> 4 RewritePageList<Model.Announced> GetRPageList(RewritePageInfo Rpi);
在BLL项目添加调用方法
View Code
1 /// <summary> 2 /// 分页获取泛型数据列表[重写] 3 /// </summary> 4 public RewritePageList<Model.Announced> GetRPageList(RewritePageInfo Rpi) 5 { 6 RewritePageList<Model.Announced> pl = dal.GetRPageList(Rpi); 7 return pl; 8 }
最后直接在Web项目下调用方式,以AspNetPager分页调用为例
View Code
1 /// <summary> 2 /// 初始化数据筛选条件 3 /// </summary> 4 private string GetContidion() 5 { 6 StringBuilder strcontidion = new StringBuilder(); 7 strcontidion.Append(" 1=1 "); 8 if (int.Parse(ddlNewSort.SelectedValue.Trim()) > 0) 9 strcontidion.Append(" and SortID = " + int.Parse(ddlNewSort.SelectedValue.Trim() + " ")); 10 if (!string.IsNullOrEmpty(txtNewsName.Text) && !string.IsNullOrEmpty(txtNewsName.Text.Trim())) 11 strcontidion.Append(" and NewsName like '%" + txtNewsName.Text.Trim() + "%'"); 12 return strcontidion.ToString(); 13 } 14 15 /// <summary> 16 /// 数据列表绑定 17 /// </summary> 18 void _dataBind() 19 { 20 int pageindex = 0; 21 if (anp.CurrentPageIndex < 1) 22 { 23 pageindex = 1; 24 } 25 else 26 { 27 pageindex = anp.CurrentPageIndex; 28 } 29 RewritePageInfo PI = new RewritePageInfo(50, pageindex, "id", "DESC", GetContidion()); 30 RewritePageList<Model.News> pl = new News().GetRPageList(PI); 31 anp.RecordCount = PI.RecordCount; 32 33 if (pl.List.Count > 0) 34 { 35 rptNewsList.DataSource = pl.List; 36 rptNewsList.DataBind(); 37 38 rptNewsList.Visible = true; 39 emptyContent.Visible = false; 40 anp.Visible = true; 41 anp.CustomInfoHTML = "第<font color=\"red\"><b>" + anp.CurrentPageIndex + "</b></font>页/共" + anp.PageCount; 42 anp.CustomInfoHTML += "页 共" + anp.RecordCount + "条记录"; 43 } 44 else 45 { 46 rptNewsList.Visible = false; 47 anp.Visible = false; 48 emptyContent.Visible = true; 49 } 50 } 51 52 /// <summary> 53 /// 分页数据 54 /// </summary> 55 protected void anp_PageChanged(object sender, EventArgs e) 56 { 57 _dataBind(); 58 }