aspnetpager使用介绍
AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,
效果如下:
下面是我如何使用AspNetPager控件进行分页处理的详细代码:
1.首先到www.webdiyer.com下载最新的AspNetPager.dll,直接在vs2005中添加引用即可。
2.在页面上注册控件,引入该控件,当然,需要在页面中使用一个数据载体,我这里使用的是repeater控件。
注册分页控件代码:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
3.分页控件页面代码:
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" OnPageChanged="AspNetPager1_PageChanged" PageSize="5" ShowNavigationToolTip="True" FirstPageText="第一页" LastPageText="最后一页" NextPageText="下一页" PrevPageText="上一页" SubmitButtonText="Go" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" Font-Bold="True" Font-Italic="False" Font-Size="Small" ShowPageIndexBox="Always"> </webdiyer:AspNetPager>
4.后台代码:
使用linq技术:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; 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.Xml.Linq; namespace ReSenGuang.news { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { initNews(); } } private void initNews() { EntityDBDataContext dc = new EntityDBDataContext(); int page = this.AspNetPager1.CurrentPageIndex; int size = this.AspNetPager1.PageSize; var newsList = dc.t_news.OrderByDescending(p => p.id).ToList(); var news = newsList.Skip(size * (page - 1)).Take(size).ToList(); if (news != null && news.Count > 0) { AspNetPager1.RecordCount = newsList.Count; newsDL.DataSource = news; newsDL.DataBind(); } else { AspNetPager1.RecordCount = 0; newsDL.DataSource = null; newsDL.DataBind(); } } protected void AspNetPager1_PageChanged(object sender, EventArgs e) { initNews(); } } }