An linq paging implementation code in asp.net
- 2020-05-16 06:45:23
- OfStack
LInq paging
Call label1.Test =GetPageNum(control name, number of bars per page, linq query statement)
Common paging
Call label1.Test =GetPageNum(datatable, control name, number of bars per page)
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();// According to the total 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; }
// Total page count
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;// Start of middle page
// Middle page termination sequence number
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } // To avoid negative Numbers in the output, set if less than 1 From the serial number 1 start
if (allpage < endcount) { endcount = allpage; } // The page number +5 The possibility that the final output sequence number will be greater than the total page number will be controlled within the page number
pagestr = " A total of " + allpage + " page  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\"> Home page </a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\"> on 1 page </a>" : " Home page on 1 page ";
// Middle page processing, which increases the time complexity and decreases 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 + "\"> Under the 1 page </a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\"> At the end of the page </a>" : " Under the 1 page At the end of the page ";
return pagestr;
}
Call label1.Test =GetPageNum(control name, number of bars per page, linq query statement)
Common paging
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; }
// Total page count
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;// Start of middle page
// Middle page termination sequence number
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } // To avoid negative Numbers in the output, set if less than 1 From the serial number 1 start
if (allpage < endcount) { endcount = allpage; } // The page number +5 The possibility that the final output sequence number will be greater than the total page number will be controlled within the page number
pagestr = " A total of " + allpage + " page  ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\"> Home page </a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\"> on 1 page </a>" : " Home page on 1 page ";
// Middle page processing, which increases the time complexity and decreases 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 + "\"> Under the 1 page </a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\"> At the end of the page </a>" : " Under the 1 page At the end of the page ";
return pagestr;
}
Call label1.Test =GetPageNum(datatable, control name, number of bars per page)