使用 DataPager 分页 ListView
如何以编程方式使用 DataPager 对 ListView 进行分页
引言
最近,我收到了一些同事/朋友关于ListView的邮件,他们希望对ListView进行分页,但是数据源必须以编程方式设置。然后,我进行了一些测试,得到了以下结果。
背景
使用数据分页器对ListView进行分页非常简单,你不需要编写任何代码。只需添加并配置一个数据源,添加一个ListView,将数据源设置为ListView。添加一个数据分页器并将其配置为ListView。完成!
运行应用程序并享受它吧!
但是,当数据源在代码中设置时,必须注意一些事项。现在,要填充ListView,你必须使用数据分页器。
使用代码
让我们尝试一下。添加一个ListView
<asp:ListView ID="ListViewProducts" runat="server" ItemPlaceholderID="ProductItem">
<ItemTemplate>
<div class="Product">
<strong>
<asp:Label runat="server" ID="LabelId" Text='<%# Eval("Id") %>'></asp:Label>
::
<asp:Label runat="server" ID="LabelName" Text='<%# Eval("Name") %>'></asp:Label>
</strong>
<br />
<em>
<asp:Label runat="server" ID="LabelDescription" Text='<%# Eval("Description") %>'></asp:Label>
</em>
</div>
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="ProductItem"></asp:PlaceHolder>
</LayoutTemplate>
<ItemSeparatorTemplate>
<hr />
</ItemSeparatorTemplate>
</asp:ListView>
然后,添加数据分页器
<asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="ListViewProducts"
PageSize="3" OnPreRender="DataPagerProducts_PreRender">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
请注意,OnPreRender
事件已实现。它用于填充ListView
protected void DataPagerProducts_PreRender(object sender, EventArgs e)
{
ProductList db = new ProductList();
this.ListViewProducts.DataSource = db.GellAll();
this.ListViewProducts.DataBind();
}
我创建了两个类来帮助这个例子
1) Product:表示一个产品项;
2) ProductList:表示一个数据库访问层。
ProductClass
using System;
/// <summary>
/// Class that represent a product item.
/// </summary>
public class Product
{
private int? _Id;
private string _Name;
private string _Descrition;
public Product(){}
public Product(int Id, string Name, string Description)
{
this._Id = Id;
this._Name = Name;
this._Descrition = Description;
}
/// <summary>
/// Product Id
/// </summary>
public int? Id
{
get { return _Id; }
set { _Id = value; }
}
/// <summary>
/// Product Name
/// </summary>
public string Name
{
get { return _Name; }
set { _Name = value; }
}
/// <summary>
/// Product Complete Description
/// </summary>
public string Description
{
get { return _Descrition; }
set { _Descrition = value; }
}
}
ProductList 类
using System;
using System.Collections.Generic;
/// <summary>
/// Simulation of a Product Database
/// </summary>
public class ProductList
{
private IList<Product> _ProductDB = new List<Product>();
public ProductList()
{
this._ProductDB.Add(new Product(1, "Computer", "Complete hardware with software included."));
this._ProductDB.Add(new Product(2, "Kitchen Calendar", "Beautiful caledar for your kitchen."));
this._ProductDB.Add(new Product(3, "Shoes", "Most advanced anti-impact system in a small shoe."));
this._ProductDB.Add(new Product(4, "Pen", "What you think, must be written. This pen is your helper."));
this._ProductDB.Add(new Product(5, "Cell Phone", "Powerfull comunication thing. Today is part of your body. Get one more."));
}
public IList<Product> GellAll()
{
return this._ProductDB;
}
}
关注点
相关文章
- http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datapager.aspx
- http://aspnet.4guysfromrolla.com/articles/021308-1.aspx
- http://www.west-wind.com/WebLog/posts/127340.aspx
历史
2008-03-03 - 首次插入。