Repeater分页(PagedDataSource)
<%@ 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>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Repeater runat="server" ID="rptShowLink">
<ItemTemplate>
<tr>
<td align="center">
<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>
</td>
<td align="center">
<%# DataBinder.Eval(Container.DataItem, "EmployeeID")%>
</td>
<td align="center">
<%# DataBinder.Eval(Container.DataItem, "OrderDate")%>
</td>
<td align="center">
<asp:LinkButton runat="server" ID="btnUpdate" Text="修改" CommandName='<%# DataBinder.Eval(Container.DataItem, "OrderID")%>' OnCommand="btnUpdate_Click" />
<asp:LinkButton runat="server" ID="btnDelete" Text="删除" CommandName='<%# DataBinder.Eval(Container.DataItem, "OrderID")%>' OnCommand="btnDelete_Click" OnClientClick="return confirm('确认删除吗?');" />
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<%--这是分隔线模板--%>
<tr>
<td colspan="4">
<hr style="border-top: 1pt;" />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td align="center">
<asp:LinkButton ID="lbtnFirstPage" runat="server" OnClick="lbtnFirstPage_Click">页首</asp:LinkButton>
<asp:LinkButton ID="lbtnpritPage" runat="server" OnClick="lbtnpritPage_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNextPage" runat="server" OnClick="lbtnNextPage_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnDownPage" runat="server" OnClick="lbtnDownPage_Click">页尾</asp:LinkButton>
<br />
第
<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>
页/共
<asp:Label ID="LabCountPage" runat="server" Text="Label"></asp:Label>
页跳至
<asp:DropDownList ID="DropDownList1" runat="server" > </asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
</table>
</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
{
SqlConnection conn = new SqlConnection("server=.;database=master;uid=sa;pwd=sa");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.labPage.Text = "1";
this.contrlRepeater();
}
}
//获取指字符个数的字符
private string cuts(string aa, int bb)
{
if (aa.Length <= bb) { return aa; }
else { return aa.Substring(0, bb); }
}
private DataSet returnDs(string sqlStr)
{
DataSet ds = new DataSet();
try
{
conn.Open();
SqlCommand comm = new SqlCommand(sqlStr, conn);
comm.CommandTimeout = 20;
SqlDataAdapter sda = new SqlDataAdapter(comm);
sda.Fill(ds, "temptable");
conn.Close();
return ds;
}
catch
{
return null;
}
}
private bool ExectueSql(string sqlstr)
{
try
{
conn.Open();
SqlCommand comm = new SqlCommand(sqlstr, conn);
int temp = comm.ExecuteNonQuery();
conn.Close();
if (temp > 0)
return true;
else
return false;
}
catch
{
return false;
}
}
//Repeater分页控制显示方法
public void contrlRepeater()
{
DataSet ss = new DataSet();
ss = returnDs("select top 200 * from test");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ss.Tables["temptable"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = Convert.ToInt32(this.labPage.Text) - 1;
rptShowLink.DataSource = pds;
LabCountPage.Text = pds.PageCount.ToString();
labPage.Text = (pds.CurrentPageIndex + 1).ToString();
this.lbtnpritPage.Enabled = true;
this.lbtnFirstPage.Enabled = true;
this.lbtnNextPage.Enabled = true;
this.lbtnDownPage.Enabled = true;
if (pds.CurrentPageIndex < 1)
{
this.lbtnpritPage.Enabled = false;
this.lbtnFirstPage.Enabled = false;
}
if (pds.CurrentPageIndex == pds.PageCount - 1)
{
this.lbtnNextPage.Enabled = false;
this.lbtnDownPage.Enabled = false;
}
rptShowLink.DataBind();
}
protected void lbtnpritPage_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) - 1);
this.contrlRepeater();
}
protected void lbtnFirstPage_Click(object sender, EventArgs e)
{
this.labPage.Text = "1";
this.contrlRepeater();
}
protected void lbtnDownPage_Click(object sender, EventArgs e)
{
this.labPage.Text = this.LabCountPage.Text;
this.contrlRepeater();
}
protected void lbtnNextPage_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
this.contrlRepeater();
}
//修改
protected void btnUpdate_Click(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandName);
}
//删除
protected void btnDelete_Click(object sender, CommandEventArgs e)
{
int Id = Convert.ToInt32(e.CommandName);
string str = "delete test where orderid=" + Id;
if (ExectueSql(str))
Response.Write("<script language='javascript'>alert('成功了');</script>");
else
{
Response.Write(@"<script language='javascript'>alert('失败了');</script>");
}
}
}
/*
if exists(select * from sysobjects where name='test')
drop table test
go
create table test
(
orderid int,
CustomerID varchar(12),
EmployeeID varchar(12),
OrderDate varchar(12)
)
insert into test values(1,'1','1','1');
insert into test values(2,'2','2','2');
insert into test values(3,'3','3','3');
insert into test values(4,'5','51','31');
insert into test values(5,'134','3451','sdfh1');
select * from tes
*/