2011-01-06 19 views
0

私はシェアポイントwebpartで簡単なページングを実装しようとしています。私はいくつかの簡単な列を持つ単一のニュース記事リストを持っています。私は、ページ上に5つ、下に数字のページングを持つことができるようにしたい。私はsplistitemcollectionpositionを理解しようとするネットを通過したが運がない。誰もがあなたが私にSharepoint 2010カスタムwebpartページング

感謝

クリス

答えて

0

を簡単なコード例や、いくつかのためのガイドラインを与えることができますしてください助けることができるならば、私はSPDataSourceとSPGridViewを使用することをお勧めし、一緒に彼らは、ページングを実装し、他の多くのクールな機能だろう最小限のコードで、またはコードなしで。

+0

これは、したがって、グリッドがために不適切であるフロントエンドのウェブサイトのためでありますこれは、Webパーツが正しい解決策である理由です。 – kalabo

+0

グリッドをカスタマイズして、必要なものを探すことができます。この魔法は、ページングバックエンドを実装するSPDataSourceにあります。UIの任意のコントロールを使用できます –

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; } 
    } 
} 
関連する問題