0
votes

I am trying to implement simple paging on my sharepoint webpart. I have a single news articles list which has some simple columns. I want to be able to have then five on a page and with some numerical paging at the bottom. I have gone through the net trying to understand splistitemcollectionposition but with no luck. If anyone can help please can you give me a simple code example or some guidanc

Many thanks

Chris

3

3 Answers

0
votes

I would suggest using SPDataSource and a SPGridView, together they will implement paging and many other cool features with minimal or no code.

0
votes

Use this a a guide for some of the classes/methods/properties you might need to use to get paging to work. Be aware that this code does not compile, i have just pulled together various code snippets that i have in my own list results framework, which includes paging, sorting, grouping and caching. It should be enough to get you started though.

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; }
    }
}
0
votes

check out my post on how to page using SPListItemCollectionPosition, I did a component to page over lists, maybe it can help -> http://hveiras.wordpress.com/2011/11/07/listpagert-using-splistitemcollectionposition/