NET paging implementation and code

Source: Internet
Author: User

Recently, I was writing a framework about NET, but when I wrote it to the end, I encountered paging. I read a lot about paging and wrote it by myself. Let's share it with you, for the first time I wrote a blog, I hope you can give me more comments...

Cs file paging code:

1 Paging p = new Paging (3, SQL statement); // 3 per page 2 ViewState ["List"] = p. getDataTable (); // query the DataTable3 ViewState ["page"] = p. getPageing (this); // display html by PAGE

Foreground call:

1 <%=ViewState["page"]%>

The ViewState function just recently found is convenient to call as long as the foreground is defined in the background. (Is this out of my memory)

The following describes the Paging class:

1 public class Paging 2 {3 private string pagedNum; // defines the page entries and outputs them to the foreground 4 private int pageSize; // defines the size of each page. 5 private string url; // get the current page 6 private int countNum; // total number of lines 7 private string SelSql; // query SQL statement 8 private DataSet ds; 9 10 public Paging (int pageSize, string SelSql) 11 {12 this. pageSize = pageSize; 13 this. url = HttpContext. current. request. url. localPath; 14 this. selSql = SelSql; 15 GetPageDat E (SelSql); 16} 17 // define the first style 18 private readonly string csstagA = @ "<a href = '{0 }? Page = {1} '> {2} </a> "; // {0} {1} {2} represents the url and parameter name, parameter value, page number 19 // define the second style of the style a label 20 // private readonly string csstagA1 = "<a style = 'font-size: 13px; font-weight: bold; margin: 0 4px 0 4px '> {0} </a> "; 21 private readonly string csstagA1 =" <span class = 'pc'> {0} </span> "; 22 23 24 public delegate int GetDelegate (); 25 26 27 /// <summary> 28 // generate 29 Page entries /// </summary> 30 /// <param name = "pageIndex"> current page </ param> 31 // <pa Ram name = "del"> method for obtaining the number of data entries </param> 32 // <returns> </returns> 33 public string GetPageing (Page p) 34 {35 string index = p. request. queryString ["page"]; 36 int I; 37 if (! String. isNullOrEmpty (index) & int. tryParse (index, out I) 38 {39 pagedNum = GetPagegNum (I, GetPageCount (I, countNum); 40} 41 else 42 {43 pagedNum = GetPagegNum (1, getPageCount (1, countNum); 44} 45 46 return pagedNum; 47} 48 49 private int GetPageCount (int pageIndex, int countPage) // obtain the total number of pages 50 {51 int Count = 0; 52 Count = countPage; 53 double c = Count * 1.0/pageSize; 54 return (int) Math. ceiling (c); 55} 56 57 58 private string GetPagegNum (int pageIndex, int pageCount) // similar to previous page 1... 7 8 9 rows 11 12 13 14 next page listing is currently selected page 59 {60 StringBuilder sb = new StringBuilder (); 61 List <int> ns = new List <int> (); // receives the number 62 string [] numList = new string [12] in the range of the current page. // 12 string arrays, store paging data 63 numList [0] = ""; // "Previous Page" position 64 numList [11] = ""; // "next page" position 65 if (pageIndex> 1) // judge the current page 66 {67 numList [0] = string. format (csstagA, url, (pageIndex-1), "<previous page"); 68} 69 if (pageIndex <pageCount) 70 {71 numList [11] = string. format (csstagA, url, (pageIndex + 1), "Next page>"); 72} 73 if (pageIndex> = 10) // status 74 {75 of the current page is greater than 10 // main 76 numList [1] = string. format (csstagA, url, 1, 1); 77 numList [2] = "... "; 78 // int index = 0; 79 if (pageIndex + 4> = pageCount) // if the number of pages added to the current page is smaller than the total number of pages 80 {81 for (int I = pageCount-7; I <pageCount + 1; I ++) 82 {83 // index = I; 84 ns. add (I); 85} 86 for (int j = 0; j <= 7; j ++) // traverse the ns page number and fill in the page number 87 {88 if (ns [j] = pageIndex) // determine whether the page number is the current page number, to use different css styles 89 {90 numList [j + 3] = string. format (csstagA1, ns [j]); // because the first three digits of the string array are "previous", "1", "... ", so fill 7 91} 92 numList [j + 3] = string from the fourth digit. format (csstagA, url, ns [j], ns [j]); 93} 94} 95 for (int I = pageIndex-3; I <= pageIndex + 4; I ++) 96 {97 // index = I; 98 ns. add (I); 99} 100 for (int j = 0; j <= 7; j ++) 101 {102 if (ns [j] = pageIndex) 103 {104 numList [j + 3] = string. format (csstagA1, ns [j]); 105} 106 else107 {108 numList [j + 3] = string. format (csstagA, url, ns [j], ns [j]); 109} 110} 111} 112 else // status below 10 pages 113 {114 if (pageCount> = 10) // The number of pages is greater than or equal to 10 115 {116 for (int I = 1; I <= 10; I ++) 117 {118 if (I = pageIndex) 119 {120 numList [I] = string. format (csstagA1, I); 121} 122 else123 {124 numList [I] = string. format (csstagA, url, I, I); 125} 126} 127} 128 else // The number of pages is less than 10 129 {130 for (int I = 1; I <= pageCount; I ++) 131 {132 if (I = pageIndex) 133 {134 numList [I] = string. format (csstagA1, I); 135} 136 else137 {138 numList [I] = string. format (csstagA, url, I, I); 139} 140} 141} 142 sb. append ("<div class = 'page'>"); 144 for (int I = 0; I <numList. length; I ++) // enter the string array in StringBulider 145 {146 sb. append (numList [I]); 147} 148 149 sb. appendFormat ("{0}/{1} in total", pageIndex, pageCount); 150 sb. append ("</div>"); 151 return sb. toString (); // return, in the foreground, <span id = "pagedspan"> <% = pagedNum %> </span> 152} 153 154 // <summary> 155 // obtain the paging data and the total number of pages 156 /// </summary> 157 // <param name = "pageSql"> </param> 158 public void GetPageDate (string pageSql) 159 {160 int pageIndex = Req. queryString ("page "). toInt (1); 161 162 StringBuilder strSql = new StringBuilder (); 163 strSql. append (@ "declare @ startRow int164 declare @ endRow int165 declare @ pageSize int166 declare @ pageIndex int167 set @ pageSize = @ parse 168 set @ pageIndex = @ parse 169 set @ startRow = (@ pageIndex-1) * @ pageSize + 1 170 set @ endRow = @ pageSize * @ pageIndex "); 171 strSql. appended ("select * from ("); 172 strSql. append ("select *, ROW_NUMBER () OVER (order by getdate () pageID from ("); 173 strSql. append (pageSql); 174 strSql. append (") tab_a"); 175 strSql. append (") tab_ B where pageID between @ startRow and @ endRow"); 176 177 strSql. append ("select COUNT (*) from ("); // query the total number of pages 178 strSql. append (pageSql); 179 strSql. append (") tab_count"); 180 181 // pagesize 182 entries per page // pageIndex Page 183 // set @ startRow = (@ pageIndex-1) * @ pageSize + 1 start page 184 // set @ endRow = @ pageSize * @ pageIndex end page 185 186 ds = DBHelper. getDataSet (strSql. toString (); 188 countNum = int. parse (ds. tables [1]. rows [0] [0]. toString ()); 189} 190 191 // <summary> 192 // page data list 193 // </summary> 194 // <returns> </returns> 195 public DataTable GetDataTable () 196 {197 return this. ds. tables [0]; 198} 199 200}View Code

The key paging SQL statements are:

Select * from (select *, ROW_NUMBER () OVER (order by getdate () pageID from Table Name) awhere pageID between (@ pageIndex-1) * @ pageSize + 1 and @ pageSize * @ pageIndex

In this example, ROW_NUMBER () OVER (order by getdate () is used to pass an ID without passing SQL statements.

In this way, the SQL paging is implemented, and finally the style similar to Baidu is written. The above csstagA and csstagA1 CSS are:

/* Paging */. page {padding-top: 10px ;}. page. pc {font-size: 13px; font-weight: bold; margin: 0 5px 0 5px ;}. page a {border: 1px solid # CFCBCB; font-size: 13px; padding: 8px 13px; margin: 0 5px 0 5px; text-decoration: none; display: inline-block ;}. page a: hover {background: # f2f8ff; border: 1px solid # 38f;}/* End paging */

The final display effect is as follows:

 

OK. The above are the detailed steps for implementing paging. O (partition _ tables) o Haha...

Related Article

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.