用PagedDataSource类实现Repeater和DataList的分页。 PagedDataSource封装了DataGrid的分页属性,我们可以象DataGrid那样进行分页。
HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body style="text-align:center">
<form id="form1" runat="server">
<div align="center" style="800px;">
<table cellpadding="0" cellspacing="1" border="0" bgcolor="#000000" width="100%">
<tr>
<td bgcolor=#ffffff>au_id</td>
<td bgcolor=#ffffff>au_fname</td>
<td bgcolor=#ffffff>au_lname</td>
<td bgcolor=#ffffff>phone</td>
<td bgcolor=#ffffff>address</td>
<td bgcolor=#ffffff>city</td>
<td bgcolor=#ffffff>state</td>
<td bgcolor=#ffffff>zip</td>
<td bgcolor=#ffffff>contract</td>
</tr>
<asp:Repeater runat="server" ID="Rpt_Test" OnItemDataBound="Rpt_Test_ItemDataBound">
<ItemTemplate>
<tr>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.au_id") %>
</td >
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.au_fname") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.au_lname") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.phone") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.address") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.city") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.state") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.zip") %>
</td>
<td bgcolor=#ffffff>
<%#DataBinder.Eval(Container,"DataItem.contract") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
<asp:Label ID="lblCurrentPage" runat="server" Text="Label" Font-Size=12px></asp:Label>
<asp:HyperLink ID="lnkPrev" runat="server" Font-Size=12px>上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server" Font-Size=12px>下一页</asp:HyperLink>
</form>
</body>
</html>
代码页面
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
#region Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
PagedDataSource objds = new PagedDataSource();
objds.DataSource = ReturnDataTable().DefaultView;
objds.AllowPaging = true;
objds.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage = 1;
}
objds.CurrentPageIndex = CurPage - 1;
lblCurrentPage.Text = "当前页:" + CurPage.ToString();
if (!objds.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
if (!objds.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
Rpt_Test.DataSource = objds;
Rpt_Test.DataBind();
}
#endregion
#region 返回数据表中数据
/// <summary>
/// 返回数据表中数据
/// </summary>
/// <returns></returns>
private DataTable ReturnDataTable()
{
DataSet ds = new DataSet();
using (SqlConnection cn = new SqlConnection("server=jhtest4;uid=sa;pwd=123;database=pubs"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "select * from authors";
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
}
return ds.Tables[0];
}
#endregion
#region Rpt_Test_ItemDataBound
/// <summary>
/// Rpt_Test_ItemDataBound
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Rpt_Test_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
#endregion
}