Repeater分页显示最普通的方法2<转>

Repeater分页显示 
简单的说,先从控件工具箱中拖入两个label(currten page当前页,count page总页),四个linkbuttion(首/下页/上页/尾页)。 
无非就是+1 -1 的问题, 
------------------------ 
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; 
using System.Data.SqlClient; 

public partial class gonggao : System.Web.UI.UserControl 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
this.labPage.Text = "1"; 
this.contrlRepeater(); 
} 
} 
//获取指字符个数的字符 
public string cuts(string aa,int bb) 
{ 
if (aa.Length <= bb) { return aa; } 
else { return aa.Substring(0, bb); } 
} 


//Repeater分页控制显示方法 
public void contrlRepeater() 
{ 
pb pb1 = new pb(); 
DataSet ss = new DataSet(); 
ss = pb1.returnDs("select top 200 [标题],[时间] from news"); 
PagedDataSource pds = new PagedDataSource(); 
pds.DataSource = ss.Tables["temptable"].DefaultView; 
pds.AllowPaging = true; 
pds.PageSize = 2; 
pds.CurrentPageIndex = Convert.ToInt32(this.labPage.Text) - 1; 
Repeater1.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; 
} 
Repeater1.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(); 
} 
} 
-------------------------------------------------------- 
aspx文件: 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="gonggao.ascx.cs" Inherits="gonggao" %> 
<table> 
<asp:Repeater ID="Repeater1" runat="server"> 
<ItemTemplate> 
<tr><td><a href=""><%# cuts(DataBinder.Eval(Container.DataItem,"[标题]").ToString(),8)%></a></td> 
<td><a href=""><%# cuts(DataBinder.Eval(Container.DataItem,"[时间]").ToString(),5)%></a></td></tr> 
</ItemTemplate> 
</asp:Repeater> 
</table> 
<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> 


附:pb.returnDs(根据SQL语句返回dataSet数据集.temptable表) 
public DataSet returnDs(string sqlStr) 
{ 

DataSet ds = new DataSet(); 
try 
{ 
//conn.Open(); 
SqlCommand comm = new SqlCommand(sqlStr, conn); 
comm.CommandTimeout = 20; 
this.conn.Open(); 
SqlDataAdapter sda = new SqlDataAdapter(comm); 
sda.Fill(ds, "temptable"); 
return ds; 
conn.Close(); conn.Dispose(); comm.Dispose(); 

} 
catch (Exception e) 
{ 
throw (e); 
// ds = null; 
return ds; 
} 
finally 
{ 
this.conn.Close(); 
} 

}

posted @ 2011-08-15 23:17  然嗄  阅读(342)  评论(0编辑  收藏  举报