Design and Implementation of ASP. NET paging Manager

Source: Internet
Author: User

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:

 
 
  1. PublicInterface IPaginationManager
  2. {
  3. Void Initialize (DataPaginationParas paras );
  4. Void Initialize (IDBAccesser accesser,
  5. IntPage_Size, string whereStr, string []
  6. Fields); // if you select all columns, fields can be passedNull 
  7.  
  8. DataTable GetPage (Int Index); // RetrieveIndexPage
  9. DataTable CurrentPage ();
  10. DataTable PrePage ();
  11. DataTable NextPage ();
  12.  
  13. IntPageCount {get ;}
  14. IntCacherSize {get;Set;}
  15. }

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:

 
 
  1. PublicClass DataPaginationParas
  2. {
  3. Public IntPageSize = 10;
  4. PublicString [] Fields = {"*"};
  5. // The column to be searched,"*"All columns
  6.  
  7. PublicString ConnectString;
  8. PublicString TableName;
  9. PublicString WhereStr;
  10. // Search criteriaWhereWords
  11.  
  12. PublicDataPaginationParas
  13. (String connStr, string tableName,
  14. String whereStr)
  15. {
  16. This. ConnectString = connStr;
  17. This. TableName = tableName;
  18. This. WhereStr = whereStr;
  19. }
  20.  
  21. # Region GetFiedString
  22. PublicString GetFiedString ()
  23. {
  24. If (this. Fields =Null)
  25. {
  26. This. Fields = new string [] {"*"};
  27. }
  28.  
  29. String fieldStrs ="";
  30.  
  31. For(IntI = 0; I
  32. {
  33. FieldStrs + =""+ This. Fields [I];
  34. If (I! = (This. Fields. Length-1 ))
  35. {
  36. FieldStrs + =",";
  37. }
  38. Else 
  39. {
  40. FieldStrs + ="";
  41. }
  42. }
  43. ReturnFieldStrs;
  44. }
  45. # Endregion
  46. }

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:

 
 
  1. PublicClass PaginationManager: IPaginationManager
  2. {
  3. Private DataPaginationParas theParas;
  4. Private IADOBase adoBase;
  5. Private DataTable curPage =Null;
  6. PrivateIntItemCount = 0;
  7. PrivateIntPageCount =-1;
  8. PrivateIntCurPageIndex =-1;
  9.  
  10. Private FixCacher fixCacher =Null;
  11. Private string fieldStrs ="";
  12.  
  13. ///
  14. /// CacheSize is smaller than or equal to 0 -- indicates no cache,
  15. Int. MaxValue -- cache all
  16. ///
  17. PublicPaginationManager (IntCacheSize)
  18. {
  19. If (cacheSize =Int. MaxValue)
  20. {
  21. This. fixCacher = new FixCacher ();
  22. }
  23. ElseIf (cacheSize> 0)
  24. {
  25. This. fixCacher = new FixCacher (cacheSize );
  26. }
  27. Else 
  28. {
  29. This. fixCacher =Null;
  30. }
  31. }
  32.  
  33. PublicPaginationManager ()
  34. {}
  35.  
  36. # Region IDataPaginationManager Member
  37. Public IntCacherSize
  38. {
  39. Get
  40. {
  41. If (this. fixCacher =Null)
  42. {
  43. Return0;
  44. }
  45. ReturnThis. fixCacher.Size;
  46. }
  47. Set 
  48. {
  49. If (this. fixCacher =Null)
  50. {
  51. This. fixCacher = new FixCacher (value );
  52. }
  53. Else 
  54. {
  55. This. fixCacher.Size= Value;
  56. }
  57. }
  58. }
  59. Public IntPageCount
  60. {
  61. Get
  62. {
  63. If (this. pageCount =-1)
  64. {
  65. String selCountStr = string. Format
  66. ("Select count (*) from {0} {1 }", This. theParas.
  67. TableName, this. theParas. WhereStr );
  68. DataSet ds = this. adoBase. DoQuery (selCountStr );
  69. This. itemCount =Int. Parse (ds. Tables [0].
  70. Rows[0] [0]. ToString ());
  71. This. pageCount = this. itemCount/this.
  72. TheParas. PageSize;
  73. If (this. itemCount % this. theParas. PageSize> 0 ))
  74. {
  75. + + This. pageCount;
  76. }
  77. }
  78. ReturnThis. pageCount;
  79. }
  80. }
  81.  
  82. ///
  83. /// GetPage retrieves the specified page
  84. ///
  85. PublicDataTable GetPage (Int Index)
  86. {
  87. If (Index= This. curPageIndex)
  88. {
  89. ReturnThis. curPage;
  90. }
  91.  
  92. If ((Index<0) | (Index> (This. PageCount-1 )))
  93. {
  94. Return Null;
  95. }
  96.  
  97. DataTable dt = this. GetCachedObject (Index);
  98.  
  99. If (dt =Null)
  100. {
  101. String selectStr = this. ConstrutSelectStr (Index);
  102. DataSet ds = this. adoBase. DoQuery (selectStr );
  103. Dt = ds. Tables [0];
  104.  
  105. This. CacheObject (Index, Dt );
  106. }
  107. This. curPage = dt;
  108. This. curPageIndex =Index;
  109. ReturnThis. curPage;
  110. }
  111.  
  112. Private DataTable GetCachedObject (Int Index)
  113. {
  114. If (this. fixCacher =Null)
  115. {
  116. Return Null;
  117. }
  118. Return(DataTable) this. fixCacher [Index];
  119. }
  120.  
  121. Private void CacheObject (Int Index, DataTable page)
  122. {
  123. If (this. fixCacher! =Null)
  124. {
  125. This. fixCacher. PutIn (Index, Page );
  126. }
  127. }
  128.  
  129. PublicDataTable CurrentPage ()
  130. {
  131. ReturnThis. curPage;
  132. }
  133.  
  134. PublicDataTable PrePage ()
  135. {
  136. ReturnThis. GetPage ((-- This. curPageIndex )); 
  137. }
  138.  
  139. PublicDataTable NextPage ()
  140. {
  141. ReturnThis. GetPage (++ this. curPageIndex ));
  142. }
  143.  
  144. Private string ConstrutSelectStr (IntPageIndex)
  145. {
  146. If (pageIndex = 0)
  147. {
  148. ReturnString. Format ("Select Top{0} {1}From
  149. {2} {3}ORDER BYID ", this. theParas. PageSize,
  150. This. fieldStrs, this. theParas. TableName,
  151. This. theParas. WhereStr );
  152. }
  153.  
  154. IntInnerCount = this. itemCount-
  155. This. theParas. PageSize * pageIndex;
  156. String innerSelStr = string. Format ("Select
  157. Top{0} {1}From{2} {3}ORDER BYIDDESC",
  158. InnerCount, this. fieldStrs, this. theParas.
  159. TableName, this. theParas. WhereStr );
  160. String outerSelStr = string. Format ("Select Top{0}
  161. *From({1}) DERIVEDTBLORDER BYID ", this.
  162. TheParas. PageSize, innerSelStr );
  163.  
  164. ReturnOuterSelStr;
  165. }
  166.  
  167. # Region Initialize
  168. PublicVoid Initialize (IDBAccesser accesser,
  169. IntPage_Size, string whereStr, string [] fields)
  170. {
  171. This. theParas = new DataPaginationParas (accesser.
  172. ConnectString, accesser. DbTableName, whereStr );
  173. This. theParas. Fields = fields;
  174. This. theParas. PageSize = page_Size;
  175.  
  176. This. fieldStrs = this. theParas. GetFiedString ();
  177. This. adoBase = new SqlADOBase (this. theParas.
  178. ConnectString );
  179. }
  180.  
  181. PublicVoid Initialize (DataPaginationParas paras)
  182. {
  183. This. theParas = paras;
  184. This. fieldStrs = this. theParas. GetFiedString ();
  185. This. adoBase = new SqlADOBase (this. theParas.
  186. ConnectString );
  187. }
  188.  
  189. # Endregion
  190. # Endregion
  191. }

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.

  1. ASP. NET features Overview (1)
  2. Introduction to ASP. NET multi-language support components
  3. Programming of ASP. NET Server controls
  4. Basics of ASP. NET mobile development (1)
  5. ASP. NET SqlDataSource Control

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.