About ASP. NET List generic paging instances

Source: Internet
Author: User

Provide an article on ASP. NET List generic paging instances. If you need it, please refer to it.

We used to build an ASP. NET Website. We usually bind DataSet to a display control, and use various means to paging, such as PagedDataSource. However, if you retrieve data from the business layer, usually a List, you cannot use the previous method to pagination.

One way is to use ObjectDataSource to bind the GridView and ListView pages. However, if you encounter DataList or Repeater, you have to write the page by yourself. The following is the List generic paging Demo I wrote yesterday. The figure shows the truth:

Run the following code:

List generic paging generic class:

The Code is as follows: Copy code

Public class ListPager: List {
Private int _ CurrentIndex;
Private int _ PageSize;
Private int _ TotalItem;
Private int _ PageCount;

Public int CurrentIndex
{
Get {return _ CurrentIndex ;}
Set {_ CurrentIndex = value ;}
}

Public int PageSize
{
Get {return _ PageSize ;}
Set {_ PageSize = value ;}
}

Public int TotalItem
{
Get {return _ TotalItem ;}
Set {_ TotalItem = value ;}
}

Public int PageCount
{
Get {return _ PageCount ;}
Set {_ PageCount = value ;}
}

Public ListPager (List list, int index, int pageSize)
{
This. _ CurrentIndex = index;
This. _ PageSize = pageSize;

Int startIndex = (this. _ CurrentIndex-1) * PageSize;
For (int I = startIndex; I <startIndex + this. _ PageSize & I <list. Count; I ++)
{
This. Add (list [I]);
}

This. _ TotalItem = list. Count;
This. _ PageCount = (this. _ TotalItem/PageSize) + 1;
}
}

Model for testing and simulated Bll:

The Code is as follows: Copy code

Public partial class NewsModel
{
Int _ Id;
String _ Title;
String _ Content;
DateTime _ Posttime;

Public int Id
{
Get {return _ Id ;}
Set {_ Id = value ;}
}

Public string Title
{
Get {return _ Title ;}
Set {_ Title = value ;}
}

Public string Content
{
Get {return _ Content ;}
Set {_ Content = value ;}
}

Public DateTime Posttime
{
Get {return _ Posttime ;}
Set {_ Posttime = value ;}
}
}

ASP. NET page Code (Part ):

The Code is as follows: Copy code

<H2>
ASP. NET generic paging <Div>
<H3>
Repeater control paging Demo <Ul>
<Asp: Repeater ID = "RptNews" runat = "server">
<ItemTemplate>
<Li>
<% # (Model. NewsModel) Container. DataItem). Id %>,
<% # (Model. NewsModel) Container. DataItem). Title %>,
<% # (Model. NewsModel) Container. DataItem). Content %>,
<% # (Model. NewsModel) Container. DataItem). Posttime %> </li>
</ItemTemplate>
</Asp: Repeater>
</Ul>
<Div>
<Asp: Label ID = "lblPageInfo" runat = "server" Text = "PageSize"> </asp: Label>
<Asp: Button ID = "btnFirstPage" runat = "server" Text = "Homepage" OnClick = "btnFirstPage_Click"/>
<Asp: Button ID = "btnPrevious" runat = "server" Text = "Previous Page" OnClick = "btnprevius_click"/>
<Asp: Label ID = "lblCurrentPage" runat = "server" Text = "Current"> </asp: Label>
/
<Asp: Label ID = "lblTotalPage" runat = "server" Text = "Total"> </asp: Label>
Page
<Asp: Button ID = "btnNext" runat = "server" Text = "next page" OnClick = "btnNext_Click"/>
<Asp: Button ID = "btnLastPage" runat = "server" Text = "last page" OnClick = "btnLastPage_Click"/>
</Div>
</Div>

ASP. NET background code:

The Code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Web. UI;
Using Common;

Namespace Web
{
Public partial class _ Default: System. Web. UI. Page
{
Private static ListPager lp = null;
Private static int currentIndex = 1;
Private static int pageSize = 5;

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
BindRepeater (1, pageSize );
}
}

Private void BindRepeater (int index, int pageSize)
{
BLLDemo. NewsBll optNews = new BLLDemo. NewsBll ();
List list = optNews. GetModels ();

Lp = new ListPager (list, index, pageSize );

RptNews. DataSource = lp;
RptNews. DataBind ();

BindPagerControls ();
}

Private void BindPagerControls ()
{
// Determine whether the previous page and next page buttons are enabled.
BtnPrevious. Enabled = currentIndex! = 1;
BtnNext. Enabled = currentIndex! = Lp. PageCount;

LblCurrentPage. Text = lp. CurrentIndex. ToString ();
LblTotalPage. Text = lp. PageCount. ToString ();
LblPageInfo. Text = String. Format ("{0} records in total, {1} records per page", lp. TotalItem, lp. PageSize );
}

Protected void btnprevius_click (object sender, EventArgs e)
{
-- CurrentIndex;
BindRepeater (currentIndex, pageSize );
BindPagerControls ();
}

Protected void btnNext_Click (object sender, EventArgs e)
{
++ CurrentIndex;
BindRepeater (currentIndex, pageSize );
BindPagerControls ();
}

Protected void btnFirstPage_Click (object sender, EventArgs e)
{
CurrentIndex = 1; // corrected to 1. It was written as 0 at the first sending, and 2B ~
BindRepeater (currentIndex, pageSize );
}

Protected void btnLastPage_Click (object sender, EventArgs e)
{
CurrentIndex = lp. PageCount;
BindRepeater (currentIndex, pageSize );
}
}
}

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.