后台代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.labPage.Text = "1";
this.fenye();
}
}
void fenye()
{
//获取数据源
PagedDataSource pds = new PagedDataSource();
DataTable ds = getalldata();
pds.DataSource = ds.DefaultView;
//设置允许分页
pds.AllowPaging = true;
//设置在单页上显示的条数
pds.PageSize = 5;
//获取或设置当前页的索引
pds.CurrentPageIndex = Int32.Parse(labPage.Text) - 1;
//绑定控件的数据源
Repeater1.DataSource = pds;
Repeater1.DataBind();
//设置页的总数
LabCountPage.Text = pds.PageCount.ToString();
//设置按钮的可操作性
lbtnDownPage.Enabled = true;
lbtnFirstPage.Enabled = true;
lbtnNextPage.Enabled = true;
lbtnpritPage.Enabled = true;
if (pds.CurrentPageIndex < 1)
{
lbtnFirstPage.Enabled = false;
lbtnpritPage.Enabled = false;
}
if (pds.CurrentPageIndex == pds.PageCount - 1)
{
lbtnDownPage.Enabled = false;
lbtnNextPage.Enabled = false;
}
}
DataTable getalldata()
{
using (SqlConnection cons = new SqlConnection("server = .; database = newsrelease; integrated security=SSPI;"))
{
string liketitle = string.Format("select * from T_News");
SqlDataAdapter da = new SqlDataAdapter(liketitle, cons);
DataTable ds = new DataTable();
da.Fill(ds);
if (ds != null)
{
return ds;
}
else
{
return null;
}
}
}
//设置首页按钮功能
protected void lbtnFirstPage_Click(object sender, EventArgs e)
{
labPage.Text = "1";
this.fenye();
}
//设置上一页按钮功能
protected void lbtnpritPage_Click(object sender, EventArgs e)
{
labPage.Text = (Int32.Parse(labPage.Text) - 1).ToString();
this.fenye();
}
//设置下一页按钮功能
protected void lbtnNextPage_Click(object sender, EventArgs e)
{
labPage.Text = (Int32.Parse(labPage.Text) + 1).ToString();
this.fenye();
}
//设置尾页按钮功能
protected void lbtnDownPage_Click(object sender, EventArgs e)
{
labPage.Text = LabCountPage.Text;
this.fenye();
}
前台调用:<%# ((DataRowView)Container.DataItem)["title"]%>替换<%# Eval("title")%>
参考:http://www.cnblogs.com/ngnetboy/archive/2012/06/14/2549019.html