今天上午研究一个多小时了终于出来了。
前台页面
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="tz_table" border="0" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<td>标题</td>
<td>创建人</td>
<td>到期日期</td>
<td>未读</td>
</tr>
</thead>
<tbody>
<asp:Repeater ID="rptNotice" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="LinkButton1" Text='<%# Eval("Title") %>' runat="server"></asp:LinkButton>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Expires") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<%-- <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" PageSize="5">
<Columns>
<asp:TemplateField HeaderText="标题">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text='<%# Eval("Title") %>' runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="时间">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Expires") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="提问人">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Author") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>--%>
<br />
<asp:Button ID="Button1" runat="server" Text="上一页" οnclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="下一页" />
</div>
</form>
</body>
</html>
然后后台绑定
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace LswTest
{
public partial class GrideView : System.Web.UI.Page
{
static int pageIndex = 1;
protected void Page_Load(object sender, EventArgs e)
{
//页面首次加载
if (!IsPostBack)
{
rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
rptNotice.DataBind();
}
}
//上一页
protected void Button1_Click(object sender, EventArgs e)
{
if (pageIndex < 1)
{
pageIndex = 1;
}
else
{
pageIndex -= 1;
}
rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
rptNotice.DataBind();
}
//下一页
protected void Button2_Click(object sender, EventArgs e)
{
if (pageIndex > 3)
{
pageIndex = 3;
}
else
{
pageIndex += 1;
}
rptNotice.DataSource = GetPagedTable(getTable(), pageIndex, 5);
rptNotice.DataBind();
}
//获取加载时的table
public DataTable getTable() {
DataTable dt = new DataTable();
//dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Title", typeof(string));//标题
dt.Columns.Add("Author", typeof(string));//创建者
dt.Columns.Add("Expires", typeof(DateTime));//到期日期
DataRow dr;
for (int i = 0; i < 15; i++)
{
dr = dt.NewRow();
dr["Title"] = "第" + i + "条数据";
dr["Author"] = "user00" + i;
dr["Expires"] = DateTime.Now.ToString();
dt.Rows.Add(dr);
}
return dt;
}
//操作上一页下一页的table
public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
{
if (PageIndex == 0)
return dt;
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = (PageIndex - 1) * PageSize;
int rowend = PageIndex * PageSize;
if (rowbegin >= dt.Rows.Count)
return newdt;
if (rowend > dt.Rows.Count)
rowend = dt.Rows.Count;
for (int i = rowbegin; i <= rowend - 1; i++)
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
}
}
后台页面的dataTable中数据是我自己模拟的数据,也可以从数据库中查询出来放到dataTable中,然后再对datatable进行操作!