2011-01-06 163 views
0

我想在我的sharepoint webpart上实现简单分页。我有一个单独的新闻文章列表,其中有一些简单的列。我希望能够在页面上有五个,并且在底部有一些数字分页。我已经通过网络试图了解splistitemcollectionposition但没有运气。如果有人可以帮助请你能不能给我一个简单的代码示例或一些guidancSharepoint 2010自定义webpart分页

非常感谢

克里斯

回答

0

我会建议使用SPDataSource和SPGridView,他们一起将实现分页等许多酷炫的功能只需很少或没有代码。

+0

这是一个前端的网站,因此网格是不合适的这使用...这就是为什么webpart是正确的解决方案。 – kalabo 2011-01-07 08:56:27

0

使用此指南获取可能需要使用的某些类/方法/属性以使分页生效。请注意,这段代码无法编译,我只是将我在自己的列表结果框架中的各种代码片断汇集在一起​​,其中包括分页,排序,分组和缓存。尽管如此,它应该足以让你开始。

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; } 
    } 
} 
相关问题