Asp.net提供了三個(gè)功能強(qiáng)大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分頁(yè)功能。相對(duì)DataGrid,DataList和Repeater控件具有更高的樣式自定義性,所以很多時(shí)候我們喜歡使用DataList或Repeater控件來(lái)顯示數(shù)據(jù)。
實(shí)現(xiàn)DataList或Repeater控件的分頁(yè)顯示有幾種方法:
1、寫(xiě)一個(gè)方法或存儲(chǔ)過(guò)程,根據(jù)傳入的頁(yè)數(shù)返回需要顯示的數(shù)據(jù)表(DataTable)
2、使用PagedDataSource類(位于System.Web.UI.WebControls命名空間里)
本篇文章主要說(shuō)怎么使用PagedDataSource類實(shí)現(xiàn)DataList和Repeater控件的分頁(yè)顯示。DataGrid控件內(nèi)部也使用了PagedDataSource類,PagedDataSource 類封裝 DataGrid 控件的屬性,這些屬性使 DataGrid 可以執(zhí)行分頁(yè)。
PagedDataSource 類的部分公共屬性:
AllowCustomPaging 獲取或設(shè)置指示是否啟用自定義分頁(yè)的值。
AllowPaging 獲取或設(shè)置指示是否啟用分頁(yè)的值。
Count 獲取要從數(shù)據(jù)源使用的項(xiàng)數(shù)。
CurrentPageIndex 獲取或設(shè)置當(dāng)前頁(yè)的索引。
DataSource 獲取或設(shè)置數(shù)據(jù)源。
DataSourceCount 獲取數(shù)據(jù)源中的項(xiàng)數(shù)。
FirstIndexInPage 獲取頁(yè)中的第一個(gè)索引。
IsCustomPagingEnabled 獲取一個(gè)值,該值指示是否啟用自定義分頁(yè)。
IsFirstPage 獲取一個(gè)值,該值指示當(dāng)前頁(yè)是否是首頁(yè)。
IsLastPage 獲取一個(gè)值,該值指示當(dāng)前頁(yè)是否是最后一頁(yè)。
IsPagingEnabled 獲取一個(gè)值,該值指示是否啟用分頁(yè)。
IsReadOnly 獲取一個(gè)值,該值指示數(shù)據(jù)源是否是只讀的。
IsSynchronized 獲取一個(gè)值,該值指示是否同步對(duì)數(shù)據(jù)源的訪問(wèn)(線程安全)。
PageCount 獲取顯示數(shù)據(jù)源中的所有項(xiàng)所需要的總頁(yè)數(shù)。
PageSize 獲取或設(shè)置要在單頁(yè)上顯示的項(xiàng)數(shù)。
VirtualCount 獲取或設(shè)置在使用自定義分頁(yè)時(shí)數(shù)據(jù)源中的實(shí)際項(xiàng)數(shù)。
這些屬性是否和DataGrid的屬性很相似?沒(méi)錯(cuò),DataGrid控件就是使用PagedDataSource類來(lái)實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示的 。下面舉個(gè)使用PagedDataSource類實(shí)現(xiàn)DataList和Repeater控件的分頁(yè)顯示的例子:
public PagedDataSource pds = new PagedDataSource();
public SqlConnection sqlcon = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings ["connectionstr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
string sql = "select * from UserInfo";
SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlcon);
sqlcon.Open();
try
{
DataSet ds = new DataSet();
sqlda.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 5;
int cpIndex = 1;
if (!String.IsNullOrEmpty(Request["pageID"])) Int32.TryParse(Request["pageID"], out cpIndex);
if (cpIndex < 1) cpIndex = 1;
if (cpIndex > pds.PageCount) cpIndex = pds.PageCount;
uxLiteralPage.Text = WritePage(); //畫(huà)出翻頁(yè)數(shù)字
pds.CurrentPageIndex = cpIndex - 1;
DataList1.DataSource = pds;
DataList1.DataBind();
}
catch (Exception error)
{
//輸出異常信息
Response.Write(error.ToString());
}
finally
{
//關(guān)閉數(shù)據(jù)連接
sqlcon.Close();
}
}
protected string WritePage()
{
string page = "";
int pageID = 1;
if (!String.IsNullOrEmpty(Request["pageID"])) Int32.TryParse(Request["pageID"], out pageID);
if (pageID < 1) pageID = 1;
if (pageID > pds.PageCount) pageID = pds.PageCount;
page = "<a href='DataListShow.aspx?pageID=1'><</a> ";
for (int i = 1; i <= pds.PageCount; i++)
{
if (i == pageID)
{
page += "<a href='DataListShow.aspx?pageID=" + Convert.ToString(i) + "' style='text-decoration:underline;'>" + Convert.ToString(i) + "</a> ";
}
else
{
page += "<a href='DataListShow.aspx?pageID=" + Convert.ToString(i) + "'>" + Convert.ToString(i) + "</a> ";
}
}
page += "<a href='DataListShow.aspx?pageID=" + pds.PageCount.ToString() + "'>></a> ";
return page;
}
聯(lián)系客服