用控件的
在前台放一个DataList控件
这里放重复的内容
在.cs后台中...
protected void bind()
{
int curpage = Convert.ToInt32(Request.QueryString["page"]);
if (curpage == null || curpage == 0) { curpage = 1; }
//使用PagedDataSource类实现DataList控件的分页功能
PagedDataSource ps = new PagedDataSource();
string sql = "select id,title,date from [news] where title<>''";
if (classid1 != null && classid1 != 0) { sql += " and classid=" + classid1; }
if (keyword1 != null) { sql += " and title like '%" + keyword1 + "%'"; }
sql += " order by date desc";
//获取数据集
DataSet ds = constr.GetDataSet(sql, "CP");
ps.DataSource = ds.Tables["CP"].DefaultView;
//是否可以分页
ps.AllowPaging = true;
//显示的数量
ps.PageSize = 20;
//取得当前页的页码
ps.CurrentPageIndex = curpage - 1;
this.lnkbtnUp.Src = "/images/back.gif";
this.lnkbtnNext.Src = "/images/next.gif";
this.lnkbtnBack.Src = "/images/next_end.gif";
this.lnkbtnOne.Src = "/images/back_start.gif";
this.btnOne.HRef = "/News.aspx?classid=" + classid1 + "&keyword=" + Server.UrlEncode(keyword1) + "&page=1";
this.btnUp.HRef = "/News.aspx?classid=" + classid1 + "&keyword=" + Server.UrlEncode(keyword1) + "&page=" + (curpage - 1);
this.btnNext.HRef = "/News.aspx?classid=" + classid1 + "&keyword=" + Server.UrlEncode(keyword1) + "&page=" + (curpage + 1);
this.btnBack.HRef = "/News.aspx?classid=" + classid1 + "&keyword=" + Server.UrlEncode(keyword1) + "&page=" + ps.PageCount;
if (curpage == 1)
{
//不显示第一页按钮
this.lnkbtnOne.Src = "/images/back_start_no.gif";
this.btnOne.HRef = "";
//不显示上一页按钮
this.lnkbtnUp.Src = "/images/back_no.gif";
this.btnUp.HRef = "";
}
if (curpage == ps.PageCount)
{
//不显示下一页
this.lnkbtnNext.Src = "/images/next_no.gif";
this.btnNext.HRef = "";
//不显示最后一页
this.lnkbtnBack.Src = "/images/next_end_no.gif";
this.btnBack.HRef = "";
}
//显示分页数量
this.labBackPage.Text = Convert.ToString(ps.PageCount);
//绑定DataList控件
this.DataList1.DataSource = ps;
this.DataList1.DataKeyField = "ID";
this.DataList1.DataBind();
}