Asp.net Linq TO SQL paging Method

Source: Internet
Author: User

Paging Method
Copy codeThe Code is as follows:
/// <Summary>
///
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "replist"> Control ID </param>
/// <Param name = "DataSource"> data source </param>
/// <Param name = "IndexPage"> current page </param>
/// <Param name = "PageSize"> Number of data entries per page </param>
/// <Param name = "PageParemart"> page search parameter like & a = a & B = B </param>
/// <Returns> </returns>
Public static string ShowPage <T> (System. Web. UI. WebControls. Repeater replist, IQueryable <T> DataSource, int IndexPage, int PageSize, string PageParemart)
{
String rtnStr = "";
Int sourceCount = DataSource. Count ();
If (sourceCount = 0) // No data in the data source
{
RtnStr = string. Empty;
}
Else
{
Int yutemp = sourceCount % PageSize;
Int pagecounts = (yutemp = 0 )? (SourceCount/PageSize): (sourceCount/PageSize + 1); // total number of pages
RtnStr = "<div style = 'width: 100%; '> <div style = 'float: left;'> page times: "+ IndexPage +" Page/"+ pagecounts +" Page, total "+ sourceCount +" records </div> ";
If (pagecounts = 1) // a total of one page of data
{
Replist. DataSource = DataSource;
RtnStr + = "[homepage] [Previous Page] [Next Page] [last page]";
}
Else
{
RtnStr + = "<div style = 'float: right; '> ";
If (IndexPage = 1) // Home Page
{
Replist. DataSource = DataSource. Take (PageSize );
RtnStr + = "[homepage] [Previous Page] <a href = '? Page = "+ (IndexPage + 1) + PageParemart +" '> [Next page] </a> <a href = '? Page = "+ (pagecounts) + PageParemart +" '> [last page] </a> ";
}
Else
{
Replist. DataSource = DataSource. Skip (IndexPage-1) * PageSize). Take (PageSize );
If (IndexPage = pagecounts) // The last page
{
RtnStr + = "<a href = '? Page = 1 "+ PageParemart +" '> [homepage] </a> <a href = '? Page = "+ (IndexPage-1) + PageParemart +" '> [Previous page] </a> [Next page] [last page] ";
}
Else
{
RtnStr + = "<a href = '? Page = 1 "+ PageParemart +" '> [homepage] </a> <a href = '? Page = "+ (IndexPage-1) + PageParemart +" '> [Previous page] </a> <a href = '? Page = "+ (IndexPage + 1) + PageParemart +" '> [Next page] </a> <a href = '? Page = "+ (pagecounts) + PageParemart +" '> [last page] </a> ";
}
}
RtnStr + = "</div> ";
}
Replist. DataBind ();
}
Return rtnStr;
}

Page call
Copy codeThe Code is as follows:
Private int PageSize = 10;
Private int IndexPage = 1;
Private string PageParemart = "";
Private void Bind ()
{
Strwhere = "1 = 1" + strwhere;
Str2 = "1 = 1" + str2;
Var a = from B in datas. fav_Awards_User select B;
Label2.Text = common. PageFen. ShowPage (replist, a, this. IndexPage, this. PageSize, this. PageParemart );
If (Label2.Text = "")
{
Label1.Visible = true;
}
}

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.