1. 下面是用AspNetPage来分页的,主要在网站在有一个列表名称为新闻列表,BindGridView是一个分页的函数。
下面的主要是对一个列表的查询进行的分页。
后台代码
EntityList<新闻列表项目> newsList;
public const int PageSize = 10;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView(GridView1.PageSize, 1);
}
}
/// <summary>
/// 绑定GridView控件
/// </summary>
/// <param name="pagesize">一页的条数</param>
/// <param name="pageindex">页码数</param>
protected void BindGridView(int pagesize, int pageindex)
{
var dc = new SPLinqDataContext(SPContext.Current.Web.Url);
newsList = dc.GetList<新闻列表项目>("新闻列表");
GridView1.PageSize = PageSize;
AspNetPager1.PageSize = PageSize;
var q = (from n in newsList
where n.Id != null
select new { n.Id,n.点击次数, n.标题 })
.Skip((pageindex - 1) * PageSize).Take(PageSize);
AspNetPager1.RecordCount = q.Count();
GridView1.DataSource = q;
GridView1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
GridView1.PageIndex = AspNetPager1.CurrentPageIndex;
BindGridView(GridView1.PageSize, AspNetPager1.CurrentPageIndex);
}
前台代码:
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<div>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageIndexBoxType="DropDownList" ShowPageIndexBox="Always"
SubmitButtonText="Go" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" CurrentPageButtonPosition="End"
CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" FirstPageText="首页"
LastPageText="尾页" NextPageText="下一页" OnPageChanged="Pager_PageChanged" PrevPageText="上一页">
</webdiyer:AspNetPager>
</div>