LInq pagination
Copy codeThe Code is as follows: testDataContext dc = new testDataContext ();
Public string GetPageNum (GridView GridViewName, int pagesize, IQueryable <test> SQL)
{
Int page;
If (HttpContext. Current. Request. QueryString ["page"]! = Null)
Page = Convert. ToInt32 (HttpContext. Current. Request. QueryString ["page"]);
Else
Page = 1;
// Var SQL = from o in dc. test select o;
Int total = SQL. Count (); // total data volume
Var sqls = SQL. Skip (page-1) * pagesize). Take (pagesize );
GridViewName. DataSource = sqls;
GridViewName. DataBind ();
Int allpage = 0;
Int next = 0;
Int pre = 0;
Int startcount = 0;
Int endcount = 0;
String pagestr = "";
If (page <1) {page = 1 ;}
// Calculate the total number of pages
If (pagesize! = 0)
{
Allpage = (total/pagesize );
Allpage = (total % pagesize )! = 0? Allpage + 1: allpage );
Allpage = (allpage = 0? 1: allpage );
}
Next = page + 1;
Pre = page-1;
Startcount = (page + 5)> allpage? Allpage-9: page-4; // The start Number of the intermediate page
// End number of the intermediate page
Endcount = page <5? 10: page + 5;
If (startcount <1) {startcount = 1;} // to avoid negative output, if it is smaller than 1, it starts from number 1.
If (allpage <endcount) {endcount = allpage;} // the possibility of page number + 5 will generate the final output sequence number greater than the total page number, then it should be controlled within the number of pages
Pagestr = "Total" + allpage + "Page & nbsp ";
Pagestr + = page> 1? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = 1 \ "> homepage </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ pre +" \ "> previous page </a>": "Previous page ";
// Intermediate page processing, which increases the time complexity and reduces the space complexity
For (int I = startcount; I <= endcount; I ++)
{
Pagestr + = page = I? "<Font color = \" # ff0000 \ ">" + I + "</font>": "<a href = \" "+ HttpContext. current. request. currentExecutionFilePath + "? Page = "+ I +" \ ">" + I + "</a> ";
}
Pagestr + = page! = Allpage? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ next +" \ "> next page </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ allpage +" \ "> last page </a>": "last page of the next page ";
Return pagestr;
}
Call label1.Test = GetPageNum (Control name, number of entries per page, and linq query statement)
Normal PagingCopy codeThe Code is as follows: public static string GetPageNum (DataTable ds, DataList datalistname, int pagesize)
{
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = ds. DefaultView;
ObjPds. AllowPaging = true;
Int total = ds. Rows. Count;
ObjPds. PageSize = pagesize;
Int page;
If (HttpContext. Current. Request. QueryString ["page"]! = Null)
Page = Convert. ToInt32 (HttpContext. Current. Request. QueryString ["page"]);
Else
Page = 1;
ObjPds. CurrentPageIndex = page-1;
Datalistname. DataSource = objPds;
Datalistname. DataBind ();
Int allpage = 0;
Int next = 0;
Int pre = 0;
Int startcount = 0;
Int endcount = 0;
String pagestr = "";
If (page <1) {page = 1 ;}
// Calculate the total number of pages
If (pagesize! = 0)
{
Allpage = (total/pagesize );
Allpage = (total % pagesize )! = 0? Allpage + 1: allpage );
Allpage = (allpage = 0? 1: allpage );
}
Next = page + 1;
Pre = page-1;
Startcount = (page + 5)> allpage? Allpage-9: page-4; // The start Number of the intermediate page
// End number of the intermediate page
Endcount = page <5? 10: page + 5;
If (startcount <1) {startcount = 1;} // to avoid negative output, if it is smaller than 1, it starts from number 1.
If (allpage <endcount) {endcount = allpage;} // the possibility of page number + 5 will generate the final output sequence number greater than the total page number, then it should be controlled within the number of pages
Pagestr = "Total" + allpage + "Page & nbsp ";
Pagestr + = page> 1? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = 1 \ "> homepage </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ pre +" \ "> previous page </a>": "Previous page ";
// Intermediate page processing, which increases the time complexity and reduces the space complexity
For (int I = startcount; I <= endcount; I ++)
{
Pagestr + = page = I? "<Font color = \" # ff0000 \ ">" + I + "</font>": "<a href = \" "+ HttpContext. current. request. currentExecutionFilePath + "? Page = "+ I +" \ ">" + I + "</a> ";
}
Pagestr + = page! = Allpage? "<A href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ next +" \ "> next page </a> <a href = \" "+ HttpContext. Current. Request. CurrentExecutionFilePath + "? Page = "+ allpage +" \ "> last page </a>": "last page of the next page ";
Return pagestr;
}
Call label1.Test = GetPageNum (datatable, control name, number of entries per page)