ASP.NET 中如何用C#语言代码实现分页功能

2024-12-01 08:41:30
推荐回答(3个)
回答1:

用控件的
在前台放一个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();
}

回答2:

在ASP.NET 里 添加个 AspNetPager.dll 的文件 网上可以下载!
分页控件!

回答3:

在.nET里有这个控件啊 不用这么麻烦的