LINQ Paging
Copy Code code 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 amount
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 Total 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;//Middle page starting serial number
Middle page Termination serial number
Endcount = Page < 5? 10:page + 5;
if (Startcount < 1) {startcount = 1;}//To avoid negative output, set if less than 1 starting from ordinal 1
if (Allpage < endcount) {endcount = allpage}//page +5 is likely to produce a final output ordinal greater than the total page number, then control it within the page count
PAGESTR = "Total" + allpage + "page";
Pagestr + = page > 1? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page=1\ "> Home </a> <a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath + "page=" + Pre + "> Prev </a>": "First prev";
Middle page processing, which increases the time complexity and reduces the complexity of the space
for (int i = Startcount i <= endcount; i++)
{
Pagestr + = page = I? "<font color=\" #ff0000 \ ">" + i + "</font>": "<a href=\" "+ HttpContext.Current.Request.CurrentExecutio Nfilepath + "page=" + i + ">" + i + "</a>";
}
Pagestr + = page!= allpage? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page= "+ Next +" \ "> Next page </a> <a h Ref=\ "" + HttpContext.Current.Request.CurrentExecutionFilePath + "" page= "+ allpage +" \ "> Last </a>": "Next last Page";
return pagestr;
}
Call Label1. Test=getpagenum (control name, number of display bars per page, LINQ query statement)
Normal page pagination
Copy Code code 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 Total 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;//Middle page starting serial number
Middle page Termination serial number
Endcount = Page < 5? 10:page + 5;
if (Startcount < 1) {startcount = 1;}//To avoid negative output, set if less than 1 starting from ordinal 1
if (Allpage < endcount) {endcount = allpage}//page +5 is likely to produce a final output ordinal greater than the total page number, then control it within the page count
PAGESTR = "Total" + allpage + "page";
Pagestr + = page > 1? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page=1\ "> Home </a> <a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath + "page=" + Pre + "> Prev </a>": "First prev";
Middle page processing, which increases the time complexity and reduces the complexity of the space
for (int i = Startcount i <= endcount; i++)
{
Pagestr + = page = I? "<font color=\" #ff0000 \ ">" + i + "</font>": "<a href=\" "+ HttpContext.Current.Request.CurrentExecutio Nfilepath + "page=" + i + ">" + i + "</a>";
}
Pagestr + = page!= allpage? "<a href=\" "+ HttpContext.Current.Request.CurrentExecutionFilePath +"? page= "+ Next +" \ "> Next page </a> <a h Ref=\ "" + HttpContext.Current.Request.CurrentExecutionFilePath + "" page= "+ allpage +" \ "> Last </a>": "Next last Page";
return pagestr;
}
Call Label1. Test=getpagenum (DataTable, control name, show number of bars per page)