Repeater和DataList控件提供了一個快速、靈活的表現數據的方式,但是,它們沒有內建的分頁功能;DataGrid控件提供了內建的分頁功能,但它的結構比較復雜。下面就用PagedDataSource類實現Repeater和DataList的分頁。 PagedDataSource封裝了DataGrid的分頁屬性,我們可以象DataGrid那樣進行分頁。代碼如下:
<%@ Page Language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.OleDb" %>
<script language="C#" runat="server">
public void Page_Load(Object src,EventArgs e) {
OleDbConnection objConn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
Server.MapPath("../ASPxWeb.mdb"));
OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Document",objConn);
DataSet ds=new DataSet();
objCommand.Fill(ds);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultVIEw;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "當前頁:" + CurPage.ToString();
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);
Repeater1.DataSource=objPds;
Repeater1.DataBind();
}
</script>
<Html>
<head>
<title>Repeater控件分頁的例子</title>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312">
<style>
P,TD,DIV,SPAN {font-size:9pt}
</style>
</head>
<body>
<form name="form1" method="POST" runat="server">