Sharepoint 2010自定义Web部件分页

Sharepoint 2010自定义Web部件分页,sharepoint,sharepoint-2010,Sharepoint,Sharepoint 2010,我正在尝试在sharepoint Web部件上实现简单分页。我有一个单一的新闻文章列表,其中有一些简单的栏目。我希望能够在一页上有五个,并且在底部有一些数字分页。我曾通过网络试图了解splistitemcollectionposition,但运气不佳。如果有人能帮忙,你能给我一个简单的代码示例或一些指导吗 非常感谢 Chris我建议使用SPDataSource和SPGridView,它们将一起使用最少或不使用代码来实现分页和许多其他很酷的功能。使用本指南了解一些可能需要使用的类/方法/属性,以使

我正在尝试在sharepoint Web部件上实现简单分页。我有一个单一的新闻文章列表,其中有一些简单的栏目。我希望能够在一页上有五个,并且在底部有一些数字分页。我曾通过网络试图了解splistitemcollectionposition,但运气不佳。如果有人能帮忙,你能给我一个简单的代码示例或一些指导吗

非常感谢


Chris

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

使用本指南了解一些可能需要使用的类/方法/属性,以使分页正常工作。请注意,这段代码并没有编译,我只是将我自己的列表结果框架中的各种代码片段汇集在一起,其中包括分页、排序、分组和缓存。这应该足以让你开始

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; }
    }
}
公共类页面列表结果:System.Web.UI.WebControls.WebParts.WebPart{
受保护的SPPagedGridView oGrid;
受保护的覆盖无效CreateChildControls(){
this.oGrid=new SPPagedGridView();
oGrid.allowpage=true;
oGrid.PageIndexChanging+=新的GridViewPageEventHandler(oGrid\u PageIndexChanging);
oGrid.PagerTemplate=null;//必须在Controls.Add(oGrid)之后调用
oGrid.PagerSettings.Mode=PagerButtons.numericsfirstlast;
oGrid.PagerSettings.PageButtonCount=3;
oGrid.PagerSettings.Position=PagerPosition.top和bottom;
base.CreateChildControls();
}
公共覆盖无效数据绑定(){
base.DataBind();
SPQuery q=新的SPQuery();
q、 RowLimit=(uint)info.PageSize;
如果(!string.IsNullOrEmpty(info.pagingfodata)){
SPListItemCollectionPosition=新的SPListItemCollectionPosition(信息分页FoData);
q、 ListItemCollectionPosition=位置;
}否则{
//第1页,不需要一个职位,使用一个职位会破坏一切
}
q、 Query=info.Caml;
SPListItemCollection items=SPContext.Current.List.GetItems(q);
FilterInfo信息=null;
字符串tmp=“”;
tmp=tmp.Replace(“,string.Empty);
tmp=tmp.Replace(“,string.Empty);
info.Caml=tmp;
info.pagingfodata=string.Empty;
info.CurrentPage=oGrid.CurrentPageIndex;
info.PageSize=oGrid.PageSize;
if(oGrid.PageIndex==0 | | oGrid.CurrentPageIndex==0){
//无所事事
}否则{
StringBuilder值=新的StringBuilder();
value.Append(“Paged=TRUE”);
AppendFormat(“&p_ID={0}”,ViewState[KEY_PagingPrefix+“ID:+oGrid.PageIndex]);
info.pagingfodata=value.ToString();
}
int pagecount=(int)Math.天花(items.Count/(double)oGrid.PageSize);
对于(inti=1;i
查看我关于如何使用SPListItemCollectionPosition进行分页的帖子,我做了一个组件来分页列表,也许它可以帮助->

这是一个前端网站,因此网格不适合此用途。这就是为什么Web部件是正确的解决方案。您可以自定义网格,使其看起来像您想要的任何东西。神奇之处在于实现分页后端的SPDataSource,您可以对UI使用任何控件