【发布时间】:2011-01-06 14:53:49
【问题描述】:
我正在尝试在我的 sharepoint webpart 上实现简单的分页。我有一个新闻文章列表,其中包含一些简单的列。我希望能够在一页上有五个,并在底部有一些数字分页。我已经通过网络试图了解 splistitemcollectionposition 但没有运气。如果有人可以提供帮助,请给我一个简单的代码示例或一些指导
非常感谢
克里斯
【问题讨论】:
标签: sharepoint sharepoint-2010
我正在尝试在我的 sharepoint webpart 上实现简单的分页。我有一个新闻文章列表,其中包含一些简单的列。我希望能够在一页上有五个,并在底部有一些数字分页。我已经通过网络试图了解 splistitemcollectionposition 但没有运气。如果有人可以提供帮助,请给我一个简单的代码示例或一些指导
非常感谢
克里斯
【问题讨论】:
标签: sharepoint sharepoint-2010
我建议使用 SPDataSource 和 SPGridView,它们将一起使用最少或无需代码实现分页和许多其他很酷的功能。
【讨论】:
将此指南用作您可能需要使用的一些类/方法/属性的指南,以使分页正常工作。请注意,此代码无法编译,我刚刚将我自己的列表结果框架中的各种代码 sn-ps 汇总在一起,其中包括分页、排序、分组和缓存。不过,这应该足以让您入门。
public class PagedListResults : System.Web.UI.WebControls.WebParts.WebPart {
protected SPPagedGridView oGrid;
protected override void CreateChildControls() {
this.oGrid = new SPPagedGridView();
oGrid.AllowPaging = true;
oGrid.PageIndexChanging += new GridViewPageEventHandler(oGrid_PageIndexChanging);
oGrid.PagerTemplate = null; // Must be called after Controls.Add(oGrid)
oGrid.PagerSettings.Mode = PagerButtons.NumericFirstLast;
oGrid.PagerSettings.PageButtonCount = 3;
oGrid.PagerSettings.Position = PagerPosition.TopAndBottom;
base.CreateChildControls();
}
public override void DataBind() {
base.DataBind();
SPQuery q = new SPQuery();
q.RowLimit = (uint)info.PageSize;
if (!string.IsNullOrEmpty(info.PagingInfoData)) {
SPListItemCollectionPosition pos = new SPListItemCollectionPosition(info.PagingInfoData);
q.ListItemCollectionPosition = pos;
} else {
//1st page, dont need a position, and using a position breaks things
}
q.Query = info.Caml;
SPListItemCollection items = SPContext.Current.List.GetItems(q);
FilterInfo info = null;
string tmp = "<View></View>";
tmp = tmp.Replace("<View><Query>", string.Empty);
tmp = tmp.Replace("</Query></View>", string.Empty);
info.Caml = tmp;
info.PagingInfoData = string.Empty;
info.CurrentPage = oGrid.CurrentPageIndex;
info.PageSize = oGrid.PageSize;
if (oGrid.PageIndex == 0 || oGrid.CurrentPageIndex == 0) {
//do nothing
} else {
StringBuilder value = new StringBuilder();
value.Append("Paged=TRUE");
value.AppendFormat("&p_ID={0}", ViewState[KEY_PagingPrefix + "ID:" + oGrid.PageIndex]);
info.PagingInfoData = value.ToString();
}
int pagecount = (int)Math.Ceiling(items.Count / (double)oGrid.PageSize);
for (int i = 1; i < pagecount; i++) { //not always ascending index numbers
ResultItem item = items[(i * oGrid.PageSize) - 1];
ViewState[KEY_PagingPrefix + "ID:" + i] = item.ID;
}
oGrid.VirtualCount = items.Count;
DateTime time3 = DateTime.Now;
DataTable table = new DataTable("Data");
DataBindListData(table, items);
this.oGrid.DataSource = table;
this.oGrid.DataBind();
this.oGrid.PageIndex = oGrid.CurrentPageIndex; //need to reset this after DataBind
}
void oGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
oGrid.PageIndex = e.NewPageIndex;
oGrid.CurrentPageIndex = oGrid.PageIndex;
}
}
public class FilterInfo {
public string Caml;
public string PagingInfoData;
public int CurrentPage;
public int PageSize;
}
public class SPPagedGridView : SPGridView {
protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource) {
pagedDataSource.AllowCustomPaging = true;
pagedDataSource.VirtualCount = virtualcount;
pagedDataSource.CurrentPageIndex = currentpageindex;
base.InitializePager(row, columnSpan, pagedDataSource);
}
private int virtualcount = 0;
public int VirtualCount {
get { return virtualcount; }
set { virtualcount = value; }
}
private int currentpageindex = 0;
public int CurrentPageIndex {
get { return currentpageindex; }
set { currentpageindex = value; }
}
}
【讨论】:
查看我关于如何使用 SPListItemCollectionPosition 进行分页的帖子,我做了一个组件来对列表进行分页,也许它可以提供帮助 -> http://hveiras.wordpress.com/2011/11/07/listpagert-using-splistitemcollectionposition/
【讨论】: