最近用简单的三层架构写代码,虽然代码比较通用,但不够个性化,闲来写了数据记录的分页类,使用起来相对比较通用(想来能写出通用高效的代码真是不容易)
列出代码:
1 public class Pagination 2 { 3 //分页总数 4 private int pageCount; 5 public int PageCount 6 { 7 get 8 { 9 return pageCount; 10 } 11 } 12 //分页大小 13 private int pageSize; 14 public int PageSize 15 { 16 get 17 { 18 return pageSize; 19 } 20 } 21 //目前页号 22 private int currentPage=1; 23 public int CurrentPage 24 { 25 get 26 { 27 return currentPage; 28 } 29 set 30 { 31 currentPage = value; 32 } 33 } 34 35 //前一页是否存在标识 36 private bool flagPrivous; 37 public bool FlagPrivous 38 { 39 get 40 { 41 if (CurrentPage < PageCount + 1 && CurrentPage != 1) 42 { 43 flagPrivous = true; 44 } 45 else 46 { 47 flagPrivous = false; 48 } 49 return flagPrivous; 50 } 51 } 52 //后一页存在标识 53 private bool flagNext; 54 public bool FlagNext 55 { 56 get 57 { 58 if (currentPage < pageCount) 59 { 60 flagNext = true; 61 } 62 else 63 { 64 flagNext = false; 65 } 66 return flagNext; 67 } 68 } 69 private string selectString; 70 private string whereString; 71 private string bllClass; 72 /// <summary> 73 /// 构造函数 74 /// </summary> 75 /// <param name="PageSize">分页的大小</param> 76 /// <param name="selectString">select子句</param> 77 /// <param name="orderByString"></param> 78 /// <param name="table">查询的表/数据库名</param> 79 public Pagination(int _PageSize,string _selectString,string _whereString,string _bllClass) 80 { 81 //初始化字段 82 this.selectString = _selectString; 83 this.whereString = _whereString; 84 this.bllClass = _bllClass; 85 this.pageSize=_PageSize; 86 87 Assembly assembly = Assembly.Load("TYKY_OA.BLL"); 88 object bllclass = assembly.CreateInstance(bllClass, false); 89 Type type = assembly.GetType(bllClass); 90 string[] param = new string[]{whereString}; 91 //获取页的总数 92 int count = (int)type.InvokeMember("getRecordCount", BindingFlags.InvokeMethod, null, bllclass, param); 93 this.pageCount = count / pageSize + ((count % pageSize == 0) ? 0 : 1); 94 } 95 96 //返回当前页 97 public DataSet GetSet() 98 { 99 Assembly assembly = Assembly.Load("TYKY_OA.BLL"); 100 object bllclass = assembly.CreateInstance(bllClass, false); 101 Type type = assembly.GetType(bllClass); 102 object[] param = new object[]{currentPage,pageSize,selectString,whereString}; 103 //获取页的总数 104 return (DataSet)type.InvokeMember("GetSet", BindingFlags.InvokeMethod, null, bllclass, param); 105 } 106 107 } 108
1 在每个DAL、BLL层添加相应的函数,例如:Salary表格进行分页 2 1.在DAL层添加: 3 public int getRecordCount(string whereString) 4 { 5 return dal.GetRecordCount(whereString); 6 } 7 public DataSet GetSet(int pageIndex, int pageSize, string selectString, string whereString) 8 { 9 return dal.GetSet(pageIndex, pageSize, selectString, whereString); 10 } 11 2.在BLL层添加 12 //查询符合条件的记录条数 13 public int getRecordCount(string whereString) 14 { 15 return dal.GetRecordCount(whereString); 16 } 17 18 public DataSet GetSet(int pageIndex, int pageSize, string selectString, string whereString) 19 { 20 return dal.GetSet(pageIndex, pageSize, selectString, whereString); 21 } 22 3.初始化分页功能 23 TYKY_OA.Web.Pagination page = new TYKY_OA.Web.Pagination(pageSize 24 , "salaryID,staffID,name,PrevSalary,PresSalary,salaryTime,salaryRange,salaryReason,appendix" 25 , "1=1" //这里是个条件 26 , "TYKY_OA.BLL.SalaryRecordInfo" 27 ); 28 this.prev.Disabled = !page.FlagPrivous; 29 this.next.Disabled = !page.FlagNext; 30 this.pageIndex.Text = "当前第" + page.CurrentPage + "页" + "(共" + page.PageCount + "页)"; 31 this.gvSalary.DataSource = page.GetSet(); 32 this.gvSalary.DataBind(); 33 Session["ASRpagenation"] = page; 34 4.实现分页 35 TYKY_OA.Web.Pagination page = (TYKY_OA.Web.Pagination)Session["ASRpagenation"]; 36 if (page != null) 37 { 38 page.CurrentPage = page.CurrentPage - 1;// -/+ 实现上下页 39 this.prev.Disabled = !page.FlagPrivous; 40 this.next.Disabled = !page.FlagNext; 41 this.pageIndex.Text = "当前第" + page.CurrentPage + "页" + "(共" + page.PageCount + "页)"; 42 this.gvSalary.DataSource = page.GetSet(); 43 this.gvSalary.DataBind(); 44 Session["ASRpagenation"] = page; 45 }
技术有限,简单记录。