Asp.net| paging provides automatic paging in the DataGrid Web version control, but I never used it, because the pagination it implements is just an illusion. Why do we need pagination? That's because qualifying records can be a lot of things, and if you read all the records at once, you're not only prolonging the time to get the data, but it's also extremely wasteful of memory. The main purpose of paging is to address both of these issues (and, of course, to use paging for the sake of the UI's aesthetic needs). And how does the Web page's DataGrid implement pagination? It is not intended to solve both of these problems, but to read all the data at once and then display it as a paging. This is a great damage to efficiency and memory!
So I implemented my own paging manager Ipaginationmanager, Ipaginationmanager each time a specified page is read from the database, and a specified number of pages can be cached. The main features of the paging manager are:
(1) Support random jump. This is done through a nested SELECT statement.
(2) caching is supported. Support through EnterpriseServerBase.DataStructure.FixCacher.
Let's look at the definition of the Ipaginationmanager interface:
public interface Ipaginationmanager { void Initialize (Datapaginationparas paras); void Initialize (Idbaccesser accesser, int page_size, String wherestr, string[] fields);//If all columns are selected, fields can pass NULL
int pagecount{get;} int cachersize{get; set;} } This interface definition, the most important is the GetPage () method, implemented this method, the other three ways to get the page currentpage, Prepage, NextPage is also very easy. In addition, the Cachersize property allows us to specify the number of cached pages. If caching is not required, set its value <=0, and if an infinite cache is required, the value is Int.maxvalue.
Ipaginationmanager interface in the second initialize method, you do not care, it is to the xcodefactory generated by the data layer used, Let's take a look at the definition of the parameter type Datapaginationparas for the first Initialize method:
public class Datapaginationparas { public int PageSize = 10; Public string[] Fields = {"*"}; The column to search for, "*" means all columns
public string connectstring; public string TableName; public string wherestr; Where words for search criteria
#region getfiedstring public string getfiedstring () { if (this. Fields = null) { This. Fields = newstring[] {"*"}; }
String fieldstrs = "";
for (int i=0; I { Fieldstrs + = "" + this. Fields[i]; if (i!=) (this. FIELDS.LENGTH-1)) { Fieldstrs + = ","; } Else { Fieldstrs + = ""; } }
return fieldstrs; } #endregion
}
Datapaginationparas.getfiedstring is used to form a string of columns to be searched for embedding into an SQL statement. The meanings of the other fields in the Datapaginationparas are obvious.
Now let's look at the paging manager implementation:
public class Paginationmanager:ipaginationmanager { Private Datapaginationparas Theparas; Private Iadobase adobase; Private DataTable curpage = null; private int itemCount = 0; private int pagecount =-1; private int curpageindex =-1;
/// CacheSize is less than or equal to 0--Indicates no caching, int.maxvalue--caches all /// Public Paginationmanager (int cacheSize) { if (cacheSize = Int. MaxValue) { This.fixcacher = new Fixcacher (); } else if (cacheSize >0) { This.fixcacher = new Fixcacher (cacheSize); } Else { This.fixcacher = null; } }
Public Paginationmanager () {}
#region Idatapaginationmanager Members public int Cachersize { Get { if (This.fixcacher = null) { return 0; } return this.fixCacher.Size; } Set { if (This.fixcacher = null) { This.fixcacher = new Fixcacher (value); } Else { This.fixCacher.Size = value; } } } public int PageCount { Get { if (This.pagecount = = 1) { String selcountstr = String. Format ("SELECT count (*) from {0} {1}", This.theParas.TableName, THIS.THEPARAS.WHERESTR); DataSet ds= this.adoBase.DoQuery (SELCOUNTSTR); this.itemcount = Int. Parse (ds. Tables[0]. Rows[0][0]. ToString ()); This.pagecount = this.itemcount/this.theparas.pagesize; if ((This.itemcount%this.theparas.pagesize >0)) { + + This.pagecount; } } return this.pagecount; } }
/// GetPage to remove the specified page /// Public DataTable getpage (int index) { if (index = = this.curpageindex) { return this.curpage; }
Public DataTable currentpage () { return this.curpage; }
Public DataTable Prepage () { return this. GetPage ((--this.curpageindex)); }
Public DataTable NextPage () { return this. GetPage ((++this.curpageindex)); }
private string construtselectstr (int pageIndex) { if (pageIndex = 0) { return string. Format (' select top {0} {1} ' from {2} {3} ' ORDER by ID ', This.theParas.PageSize, This.fieldstrs, this.theParas.TableName, thi S.THEPARAS.WHERESTR); }
int innercount = This.itemcount-this.theparas.pagesize*pageindex; String innerselstr = String. Format (' select top {0} {1} ' from {2} {3} ' ORDER by ID DESC ', Innercount, This.fieldstrs, This.theParas.TableName, this.the PARAS.WHERESTR); String outerselstr = String. Format ("Select top {0} * out ({1}) derivedtbl ORDER by ID", This.theParas.PageSize, INNERSELSTR);
return outerselstr; }
#region Initialize public void Initialize (Idbaccesser accesser, int page_size, String wherestr, string[] fields) { This.theparas = new Datapaginationparas (accesser. ConnectString, Accesser. Dbtablename, WHERESTR); This.theParas.Fields = Fields; This.theParas.PageSize = page_size;
This.fieldstrs = This.theParas.GetFiedString (); This.adobase = new Sqladobase (this.theParas.ConnectString); }
public void Initialize (Datapaginationparas paras) { This.theparas = paras; This.fieldstrs = This.theParas.GetFiedString (); This.adobase = new Sqladobase (this.theParas.ConnectString); }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.