简体   繁体   English

Sharepoint 2010自定义WebPart分页

[英]Sharepoint 2010 custom webpart paging

I am trying to implement simple paging on my sharepoint webpart. 我正在尝试在共享点Web部件上实现简单的分页。 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. 我通过网络试图了解splistitemcollectionposition,但是没有运气。 If anyone can help please can you give me a simple code example or some guidanc 如果有人可以帮助您,请给我一个简单的代码示例或一些guidanc。

Many thanks 非常感谢

Chris 克里斯

我建议使用SPDataSource和SPGridView,它们一起可以用最少的代码或根本不用代码来实现分页和许多其他很酷的功能。

Use this aa 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; }
    }
}

查看我有关如何使用SPListItemCollectionPosition进行分页的文章,我做了一个组件来对列表进行分页,也许它可以帮助-> http://hveiras.wordpress.com/2011/11/07/listpagert-using-splistitemcollectionposition/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM