The Entity framework background uses paging to fetch data and use of Aspnetpager controls

Source: Internet
Author: User

Reprint to: http://www.cnblogs.com/jaxu/p/3655582.html

This article is a note for use with Aspnetpager controls!

For Aspnetpager controls, you can view the Yang Tao home page. This is an open custom ASP. Web control that supports a variety of custom data paging methods, is easy to use and powerful, and can be used to save a lot of unnecessary hassle when using this control during site development.

Download this page: Aspnetpager745dll.zip

Then take a look at how the page data is passed to the control through the Entity Framework to implement true paging.

Page front-end code:

<Asp:listviewId= "Listview_alternative"Runat= "Server"Itemplaceholderid= "Layouttabletemplate"><LayoutTemplate><TableClass= "Infolist_table"Border= "0"cellpadding= "7"CellSpacing= "2"><Asp:placeholderId= "Layouttabletemplate"Runat= "Server"></Asp:placeholder></Table><DivClass= "Navcateline"></Div></LayoutTemplate><ItemTemplate><Tr><Td><AHref= ' <% #string. Concat ("./info.aspx?id=",Eval ("ID"))%> ' title= '<%#Eval("Title")%> ' target= "_self" ><%#Eval("Title"). ToString ()%></A></Td><TdClass= "Infolist_description"><AHref= ' <% #string. Concat ("./info.aspx?id=",Eval ("ID"))%> ' target= ' _self ><%#Eval("Description"). ToString ()%></A></Td><TdClass= "Infolist_datetime"><%#Eval("Postdatetime")%></Td></Tr></ItemTemplate><EmptyDataTemplate><DivClass= "Prodemptylist"> No related content!</Div></EmptyDataTemplate></Asp:listview><Webdiyer:aspnetpagerId= "AspNetPager1"Runat= "Server"Urlpaging= "true"Showpageindexbox= "Always"  Pageindexboxtype = "DropDownList"  Textbeforepageindexbox= "Jump to"  Textafterpageindexbox= "page"  Horizontalalign= "Center"  Pageindexboxstyle= "width:45px"  Pagesize= "15"  Cssclass=" pager " Onpagechanged= "aspnetpager1_pagechanged" ></< Span style= "color: #800000;" >webdiyer:aspnetpager>         

Page Background code:

Protectedvoid Page_Load (Objectsender, EventArgs e) {if (!Page.IsPostBack) {using (var context =NewDBEntities ()) {aspnetpager1.recordcount = Context.infolist.Where (n = n.delstate! ="True"). Count (); } }}Privatevoid Binddata (int PageSize,IntPageNumber) {using (var context =NewDBEntities ()) {var infolist =From infoInchContext.infolist.AsEnumerable ()where info.delstate! ="trueorderby Info.posttime descending select new {ID = Info.id, Title = Info.title, Description = Info.description, postdatetime = Info.posttime}; Listview_alternative. DataSource = Infolist.topagedquery (pageSize, pagenumber); listview_alternative. DataBind (); }}protected void aspnetpager1_pagechanged (object sender, EventArgs e) {binddata (aspnetpager1.pagesize, Aspnetpager1.currentpageindex)}             

It's easy to use! First you need to tell Aspnetpager how many records are in the data you want to bind, and then get the paging data through the Aspnetpager_pagechanged event. Aspnetpager can customize a variety of different paging methods, or you can pass the paging parameter through a URL, and use the method to view the help document on the Yang Tao home page. The following shows the code for how to get the Entity Framework to return paged query results.

PublicStaticClassiqueryableextensions{///<summary>///Get the paged Records.///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Query" ></param>///<param name= "PageSize" ></param>///<param name= "PageNumber" ></param>///<returns></returns>PublicStatic Iqueryable<t> topagedquery<t> (This iqueryable<t> query,int PageSize,IntPageNumber) {return query. Skip (PageSize * (PageNumber-1)). Take (pageSize); }///<summary>///Get the paged Records.///</summary>///<typeparam name= "T" ></typeparam>//////<param name= "pageSize" ></param> /// Span style= "color: #808080;" ><param name= "pagenumber" ></param> /// <returns></returns> public static Ienumerable<t> topagedquery<t> (this ienumerable<t> query, int pageSize, int PageNumber) {return query. Skip (PageSize * (pagenumber-1 

It is also very simple to use the skip () method and the Take () method to get the data after paging, and the Entity framework knows how to get the correct data record in the database. The Iqueryableextensions class in the above code contains two methods, the extension methods for IQueryable generics and ienumable generics, respectively.

Then, you can enjoy powerful data paging on the 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.