asp.net a LINQ pagination Implementation code _ Practical Tips

Source: Internet
Author: User
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)

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.