ASP.NET 中如何用C#语言代码实现分页功能
要实现:共几页、当前第几页、首页、上一页、下一页、末页、跳转到第几页等功能高手指教啊,最好能给出源代码!...
要实现:共几页、当前第几页、首页、上一页、下一页、末页、跳转到第几页等功能
高手指教啊,最好能给出源代码! 展开
高手指教啊,最好能给出源代码! 展开
3个回答
展开全部
用控件的
在前台放一个DataList控件
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
这里放重复的内容
</ItemTemplate>
</asp:DataList>
<div id="pages">当前页次:<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>/<asp:Label ID="labBackPage" runat="server"></asp:Label>
<a id="btnOne" runat="server"><img src="/images/back_start.gif" width="22" height="11" id="lnkbtnOne" runat="server" alt="首页" /></a>
<a id="btnUp" runat="server"><img src="/images/back.gif" width="16" height="11" id="lnkbtnUp" runat="server" alt="上一页" /></a>
<a id="btnNext" runat="server"><img src="/images/next.gif" width="16" height="11" id="lnkbtnNext" runat="server" alt="下一页" /></a>
<a id="btnBack" runat="server"><img src="/images/next_end.gif" width="22" height="11" id="lnkbtnBack" runat="server" alt="尾页" /></a>
</div>
在.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();
}
在前台放一个DataList控件
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
这里放重复的内容
</ItemTemplate>
</asp:DataList>
<div id="pages">当前页次:<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>/<asp:Label ID="labBackPage" runat="server"></asp:Label>
<a id="btnOne" runat="server"><img src="/images/back_start.gif" width="22" height="11" id="lnkbtnOne" runat="server" alt="首页" /></a>
<a id="btnUp" runat="server"><img src="/images/back.gif" width="16" height="11" id="lnkbtnUp" runat="server" alt="上一页" /></a>
<a id="btnNext" runat="server"><img src="/images/next.gif" width="16" height="11" id="lnkbtnNext" runat="server" alt="下一页" /></a>
<a id="btnBack" runat="server"><img src="/images/next_end.gif" width="22" height="11" id="lnkbtnBack" runat="server" alt="尾页" /></a>
</div>
在.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();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在ASP.NET 里 添加个 AspNetPager.dll 的文件 网上可以下载!
分页控件!
分页控件!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在.nET里有这个控件啊 不用这么麻烦的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询