The automatic paging function is provided in the web control of the DataGrid, but I have never used it, because the paging implemented by it is just a false phase. Why do we need paging? That's because there may be a lot of qualified records. If you read all records at a time, it not only prolongs the data acquisition time, but it also wastes a lot of memory. The main purpose of pagination is to solve these two problems. Of course, it is not ruled out that pagination is used for the sake of UI appearance ). But how does the web version of DataGrid implement paging? It does not intend to solve the above two problems, but it still reads all the data at a time and then displays it in pages. This is a huge damage to efficiency and memory!
So I implemented the ASP. NET page manager IPaginationManager. IPaginationManager reads any specified page from the database each time, and can cache a specified number of pages. The main features of this paging manager are:
1) supports random jump. This is achieved through nested Select statements.
2) supports caching. Supported by EnterpriseServerBase. DataStructure. FixCacher.
Let's take a look at the definition of the IPaginationManager interface:
- PublicInterface IPaginationManager
- {
- Void Initialize (DataPaginationParas paras );
- Void Initialize (IDBAccesser accesser,
- IntPage_Size, string whereStr, string []
- Fields); // if you select all columns, fields can be passedNull
-
- DataTable GetPage (Int Index); // RetrieveIndexPage
- DataTable CurrentPage ();
- DataTable PrePage ();
- DataTable NextPage ();
-
- IntPageCount {get ;}
- IntCacherSize {get;Set;}
- }
In this interface definition, the most important thing is the GetPage () method, which implements this method, and the other three methods for obtaining the page are very easy: CurrentPage, PrePage, and NextPage. In addition, the CacherSize attribute allows us to specify the number of cached pages. If no cache is required, set its value to <= 0. If you need unlimited cache, the value is Int. MaxValue.
The second Initialize method in the IPaginationManager interface is used by the data layer generated by XCodeFactory. Let's take a look at the definition of the parameter type DataPaginationParas of the first Initialize method:
- PublicClass DataPaginationParas
- {
- Public IntPageSize = 10;
- PublicString [] Fields = {"*"};
- // The column to be searched,"*"All columns
-
- PublicString ConnectString;
- PublicString TableName;
- PublicString WhereStr;
- // Search criteriaWhereWords
-
- PublicDataPaginationParas
- (String connStr, string tableName,
- String whereStr)
- {
- This. ConnectString = connStr;
- This. TableName = tableName;
- This. WhereStr = whereStr;
- }
-
- # Region GetFiedString
- PublicString GetFiedString ()
- {
- If (this. Fields =Null)
- {
- This. Fields = new string [] {"*"};
- }
-
- String fieldStrs ="";
-
- For(IntI = 0; I
- {
- FieldStrs + =""+ This. Fields [I];
- If (I! = (This. Fields. Length-1 ))
- {
- FieldStrs + =",";
- }
- Else
- {
- FieldStrs + ="";
- }
- }
- ReturnFieldStrs;
- }
- # Endregion
- }
DataPaginationParas. GetFiedString is used to form a string for the columns to be searched to be embedded into the SQL statement. Other fields in DataPaginationParas have obvious meanings.
Now let's take a look at the implementation of ASP. NET paging Manager:
- PublicClass PaginationManager: IPaginationManager
- {
- Private DataPaginationParas theParas;
- Private IADOBase adoBase;
- Private DataTable curPage =Null;
- PrivateIntItemCount = 0;
- PrivateIntPageCount =-1;
- PrivateIntCurPageIndex =-1;
-
- Private FixCacher fixCacher =Null;
- Private string fieldStrs ="";
-
- ///
- /// CacheSize is smaller than or equal to 0 -- indicates no cache,
- Int. MaxValue -- cache all
- ///
- PublicPaginationManager (IntCacheSize)
- {
- If (cacheSize =Int. MaxValue)
- {
- This. fixCacher = new FixCacher ();
- }
- ElseIf (cacheSize> 0)
- {
- This. fixCacher = new FixCacher (cacheSize );
- }
- Else
- {
- This. fixCacher =Null;
- }
- }
-
- PublicPaginationManager ()
- {}
-
- # Region IDataPaginationManager Member
- Public IntCacherSize
- {
- Get
- {
- If (this. fixCacher =Null)
- {
- Return0;
- }
- ReturnThis. fixCacher.Size;
- }
- Set
- {
- If (this. fixCacher =Null)
- {
- This. fixCacher = new FixCacher (value );
- }
- Else
- {
- This. fixCacher.Size= Value;
- }
- }
- }
- Public IntPageCount
- {
- 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;
- }
- }
- ReturnThis. pageCount;
- }
- }
-
- ///
- /// GetPage retrieves the specified page
- ///
- PublicDataTable GetPage (Int Index)
- {
- If (Index= This. curPageIndex)
- {
- ReturnThis. curPage;
- }
-
- If ((Index<0) | (Index> (This. PageCount-1 )))
- {
- Return Null;
- }
-
- DataTable dt = this. GetCachedObject (Index);
-
- If (dt =Null)
- {
- String selectStr = this. ConstrutSelectStr (Index);
- DataSet ds = this. adoBase. DoQuery (selectStr );
- Dt = ds. Tables [0];
-
- This. CacheObject (Index, Dt );
- }
- This. curPage = dt;
- This. curPageIndex =Index;
- ReturnThis. curPage;
- }
-
- Private DataTable GetCachedObject (Int Index)
- {
- If (this. fixCacher =Null)
- {
- Return Null;
- }
- Return(DataTable) this. fixCacher [Index];
- }
-
- Private void CacheObject (Int Index, DataTable page)
- {
- If (this. fixCacher! =Null)
- {
- This. fixCacher. PutIn (Index, Page );
- }
- }
-
- PublicDataTable CurrentPage ()
- {
- ReturnThis. curPage;
- }
-
- PublicDataTable PrePage ()
- {
- ReturnThis. GetPage ((-- This. curPageIndex ));
- }
-
- PublicDataTable NextPage ()
- {
- ReturnThis. GetPage (++ this. curPageIndex ));
- }
-
- Private string ConstrutSelectStr (IntPageIndex)
- {
- If (pageIndex = 0)
- {
- ReturnString. Format ("Select Top{0} {1}From
- {2} {3}ORDER BYID ", this. theParas. PageSize,
- This. fieldStrs, this. theParas. TableName,
- This. theParas. WhereStr );
- }
-
- IntInnerCount = this. itemCount-
- This. theParas. PageSize * pageIndex;
- String innerSelStr = string. Format ("Select
- Top{0} {1}From{2} {3}ORDER BYIDDESC",
- InnerCount, this. fieldStrs, this. theParas.
- TableName, this. theParas. WhereStr );
- String outerSelStr = string. Format ("Select Top{0}
- *From({1}) DERIVEDTBLORDER BYID ", this.
- TheParas. PageSize, innerSelStr );
-
- ReturnOuterSelStr;
- }
-
- # Region Initialize
- PublicVoid Initialize (IDBAccesser accesser,
- IntPage_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 );
- }
-
- PublicVoid Initialize (DataPaginationParas paras)
- {
- This. theParas = paras;
- This. fieldStrs = this. theParas. GetFiedString ();
- This. adoBase = new SqlADOBase (this. theParas.
- ConnectString );
- }
-
- # Endregion
- # Endregion
- }
The implementation of this class can start with the GetPage (int index) method, and the implementation of the Private method ConstrutSelectStr () illustrates how to use nested SQL statements for random paging search.
Finally, for the paging manager, it should be noted that the corresponding table to be searched must have a primary key named "ID"-this is the only requirement. In addition, the low-level encapsulation of data access used by the paging manager is defined in the EnterpriseServerBase class library.
Using ASP. NET paging manager is very simple. After adding the UI, you only need to bind the returned DataTable to the DataGrid.
- ASP. NET features Overview (1)
- Introduction to ASP. NET multi-language support components
- Programming of ASP. NET Server controls
- Basics of ASP. NET mobile development (1)
- ASP. NET SqlDataSource Control