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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body style="font:12px tahoma;">
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="font:18px tahoma;border-collapse:collapse;">
<tr>
<td style="border:1px solid #cccccc;">新闻分类</td><td style="border:1px solid #cccccc; text-align :center ">标题</td><td style="border:1px solid #cccccc;">更新时间</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border:1px solid #cccccc;"><asp:Label ID="Label1" runat="server" Text='<%#Eval("Category","{0}") %>' /></td>
<td style="border:1px solid #cccccc;"><asp:Label ID="Label2" runat="server" Text='<%#Eval("Title","{0}") %>' /></td>
<td style="border:1px solid #cccccc;"><asp:Label ID="Label3" runat="server" Text='<%#Eval("createtime","{0}") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
当前页:<asp:Label ID="lblCurPage" runat="server" Text="1" />
总共:<asp:Label ID="lblTotalPage" runat="server" Text="Label" />
<asp:LinkButton ID="lbtnFirstPage" runat="server" OnClick="lbtnFirstPage_Click" >第一页</asp:LinkButton>
<asp:LinkButton ID="lbtnUpPage" runat="server" OnClick="lbtnUpPage_Click" >上一页</asp:LinkButton>
<span id="span1" runat="server"></span>
<asp:LinkButton ID="lbtnDownPage" runat="server" OnClick="lbtnDownPage_Click" >下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnLastPage" runat="server" OnClick="lbtnLastPage_Click" >最后一页</asp:LinkButton>
</form>
</body>
</html>
cs代码:
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.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
dlBind();
}
public void dlBind()
{
int curpage = Convert.ToInt32(this.lblCurPage.Text);
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|data.mdb");
//先定义一个PageDataSource,分页全靠它
PagedDataSource ps = new PagedDataSource();
myConn.Open();
//定义一个OleDbDataAdapter
OleDbDataAdapter myAdapter = new OleDbDataAdapter("select Category,Title,createtime from [data]", myConn);
DataSet ds = new DataSet();
//填充数据
myAdapter.Fill(ds, "mytable");
ps.DataSource = ds.Tables["mytable"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3;
ps.CurrentPageIndex = curpage - 1;
this.lbtnFirstPage.Enabled = true;
this.lbtnUpPage.Enabled = true;
this.lbtnDownPage.Enabled = true;
this.lbtnLastPage.Enabled = true;
if (curpage == 1)//已经是第一页
{
this.lbtnFirstPage.Enabled = false;
this.lbtnUpPage.Enabled = false;
}
if (curpage == ps.PageCount)//已经是末页
{
this.lbtnLastPage.Enabled = false;
this.lbtnDownPage.Enabled = false;
}
this.lblTotalPage.Text = Convert.ToString(ps.PageCount);
this.Repeater1.DataSource = ps;
this.Repeater1.DataBind();
myConn.Close();
}
protected void lbtnFirstPage_Click(object sender, EventArgs e)
{
this.lblCurPage.Text = Convert.ToString(1);
this.dlBind();
}
protected void lbtnUpPage_Click(object sender, EventArgs e)
{
this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) - 1);
this.dlBind();
}
protected void lbtnDownPage_Click(object sender, EventArgs e)
{
this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) + 1);
this.dlBind();
}
protected void lbtnLastPage_Click(object sender, EventArgs e)
{
this.lblCurPage.Text = this.lblTotalPage.Text;
this.dlBind();
}
}
数据库: