本文介绍使用PagedDataSource类为Web控件Repeater和DataList实现分页。 PagedDataSource封装数据绑定控件(如 DataGrid、GridView、DetailsView 和 FormView)的与分页相关的属性,以允许该控件执行分页操作。下面以一个实例来介绍如何实现分页:
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!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>
<style type="text/css">
BODY { MARGIN: 0px }
TD { FONT-SIZE: 13px }
</style>
</head>
<body bgcolor="#f2ead5" ms_positioning="GridLayout" style="font-size: small;">
<form id="form1" runat="server">
<div>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td style="border-right: white thin groove; border-top: white thin groove; padding-left: 5px;
border-left: white thin groove; padding-top: 2px; border-bottom: white thin groove"
valign="middle" align="left" width="100%" bgcolor="#ba8439" height="22">
<font face="宋体" color="white" size="2">系统公告</font></td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
<div align="center">
<asp:HyperLink ID="hlFistPage" runat="server" ToolTip="第一页"><<</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server" ToolTip="上一页"><</asp:HyperLink>
<asp:Label ID="lblCurrentPage" runat="server" Text="Label"></asp:Label>
<asp:HyperLink ID="lnkNext" runat="server" ToolTip="下一页">></asp:HyperLink>
<asp:HyperLink ID="hlLastPage" runat="server" ToolTip="最后一页">>></asp:HyperLink><br />
<br />
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<p>
<asp:Label ID="A_TimeLabel" runat="server" Text='<%# Eval("A_Time", "{0:d}") %>'></asp:Label><br />
<br />
<asp:Label ID="ContentsLabel" runat="server" Text='<%# Eval("Contents") %>'></asp:Label></p>
<br />
</ItemTemplate>
<AlternatingItemStyle BackColor="White" />
</asp:DataList>
</form>
</body>
</html>
<!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>
<style type="text/css">
BODY { MARGIN: 0px }
TD { FONT-SIZE: 13px }
</style>
</head>
<body bgcolor="#f2ead5" ms_positioning="GridLayout" style="font-size: small;">
<form id="form1" runat="server">
<div>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td style="border-right: white thin groove; border-top: white thin groove; padding-left: 5px;
border-left: white thin groove; padding-top: 2px; border-bottom: white thin groove"
valign="middle" align="left" width="100%" bgcolor="#ba8439" height="22">
<font face="宋体" color="white" size="2">系统公告</font></td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
<div align="center">
<asp:HyperLink ID="hlFistPage" runat="server" ToolTip="第一页"><<</asp:HyperLink>
<asp:HyperLink ID="lnkPrev" runat="server" ToolTip="上一页"><</asp:HyperLink>
<asp:Label ID="lblCurrentPage" runat="server" Text="Label"></asp:Label>
<asp:HyperLink ID="lnkNext" runat="server" ToolTip="下一页">></asp:HyperLink>
<asp:HyperLink ID="hlLastPage" runat="server" ToolTip="最后一页">>></asp:HyperLink><br />
<br />
</div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<p>
<asp:Label ID="A_TimeLabel" runat="server" Text='<%# Eval("A_Time", "{0:d}") %>'></asp:Label><br />
<br />
<asp:Label ID="ContentsLabel" runat="server" Text='<%# Eval("Contents") %>'></asp:Label></p>
<br />
</ItemTemplate>
<AlternatingItemStyle BackColor="White" />
</asp:DataList>
</form>
</body>
</html>
程序代码:(这里假定已存在数据集bgsDataSet和表Affiche)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bgsDataSet ds = new bgsDataSet();
bgsDataSetTableAdapters.AfficheTableAdapter ta =
new bgsDataSetTableAdapters.AfficheTableAdapter();
ta.Fill(ds.Affiche);
ds.Affiche.DefaultView.Sort = "SN DESC";
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Affiche.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 5;
int CurretPage;
if (Request.QueryString["Page"] != null)
CurretPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurretPage = 1;
pds.CurrentPageIndex = CurretPage - 1;
lblCurrentPage.Text = "第 " + CurretPage.ToString() + " 页 / 共 " +
pds.PageCount + " 页";
if (!pds.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurretPage - 1);
hlFistPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
}
if (!pds.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurretPage + 1);
hlLastPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(pds.PageCount);
}
DataList1.DataSource = pds;
DataList1.DataBind();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bgsDataSet ds = new bgsDataSet();
bgsDataSetTableAdapters.AfficheTableAdapter ta =
new bgsDataSetTableAdapters.AfficheTableAdapter();
ta.Fill(ds.Affiche);
ds.Affiche.DefaultView.Sort = "SN DESC";
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Affiche.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 5;
int CurretPage;
if (Request.QueryString["Page"] != null)
CurretPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurretPage = 1;
pds.CurrentPageIndex = CurretPage - 1;
lblCurrentPage.Text = "第 " + CurretPage.ToString() + " 页 / 共 " +
pds.PageCount + " 页";
if (!pds.IsFirstPage)
{
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurretPage - 1);
hlFistPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
}
if (!pds.IsLastPage)
{
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurretPage + 1);
hlLastPage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(pds.PageCount);
}
DataList1.DataSource = pds;
DataList1.DataBind();
}
}
本文展示了一个使用PagedDataSource类实现ASP.NET Web控件DataList分页的示例。通过设置PagedDataSource属性,实现了数据分页显示,并提供了导航链接以切换不同页面。
785

被折叠的 条评论
为什么被折叠?



