site = new SPSite("http://MossSvr");
web = site.AllWebs["news"];
list = web.Lists["MyList"];
query = new SPQuery();
//检索所有的项目
query.ViewAttributes = "Scope='RecursiveAll'";
query.ViewFields = "<FieldRef Name='ID'/><FieldRef Name='FSObjType'/><FieldRef Name='Title'/>";
//使用查询和排序(分页中通常会遇到的)
query.Query = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'> 项目</Value></Eq></Where><OrderBy><FieldRef Name=\"Title\" Ascending=\"false\"/></OrderBy>" ;
int i = 1;
query.RowLimit = (uint)iPageSize;
do
{
SPListItemCollection listItems = list.GetItems(query);
Response.Write("第" + i.ToString() + "页<br/>");
//Response.Write(SPlicp.PagingInfo + "<br/>");
foreach (SPListItem listItem in listItems)
{
Response.Write(listItem["ID"].ToString() + "." + SPEncode.HtmlEncode(listItem["Title"].ToString()) + "<BR/>");
}
query.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
try
{
Response.Write(listItems.ListItemCollectionPosition.PagingInfo + "<br/>");
}
catch
{
}
i++;
}
while (query.ListItemCollectionPosition != null);
//Finalize
web.Dispose();
site.Dispose();<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QueryPager.aspx.cs" Inherits="QueryPager" enableSessionState="True"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Large List Paging</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField="Title" HeaderText="标题"/>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td valign="middle" align="center">
<asp:DropDownList ID="ddlPageNumber" runat="server" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
public partial class QueryPager : System.Web.UI.Page
{
/// <summary>
/// 每页显示的行数
/// </summary>
int iPageSize = 5;
/// <summary>
/// 每次显示的页数
/// </summary>
int iPages = 5;
int iRowsCount = 0;
int iPageCount = 0;
SPSite site;
SPWeb web;
SPList list;
SPQuery query;
protected void Page_Load(object sender, EventArgs e)
{
site = new SPSite("http://MossSvr");
web = site.AllWebs["news"];
list = web.Lists["MyList"];
query = new SPQuery();
query.ViewAttributes = "Scope='Recursive'";
if (Page.IsPostBack)
{
//提交时
if (ddlPageNumber.SelectedItem.Text == "more")
{
//显示更多
GetResults(iPageSize * iPages, true, ddlPageNumber.SelectedItem.Value);
}
else
{
GetResults(iPageSize, false, ddlPageNumber.SelectedItem.Value);
}
}
else
{
//首次加载时
//将返回的行数设置为每次显示页数的最多
GetResults(iPageSize * iPages, true, string.Empty);
}
//Response.Write(SPEncode.HtmlEncode(""));
//Finalize
web.Dispose();
site.Dispose();
}
private void GetResults(int iRowLimit,Boolean bMore,string sPageInfo)
{
query.RowLimit = (uint)iRowLimit;
//定位
if (sPageInfo.Length > 0)
{
query.ListItemCollectionPosition = new SPListItemCollectionPosition(sPageInfo);
}
SPListItemCollection listitems = list.GetItems(query);
iRowsCount = listitems.Count;
DataTable dtResults = listitems.GetDataTable();
if (bMore)
{
iPageCount = (int)Math.Ceiling(iRowsCount / (decimal)iPageSize);
int ddlListItemCount = ddlPageNumber.Items.Count;
if (ddlListItemCount > 0)
{
//删除More
ddlListItemCount--;
ddlPageNumber.Items.Remove(ddlPageNumber.Items[ddlListItemCount]);
}
if (iRowsCount > 0)
{
int iRowNum = 0;
SPListItem listitem = null;
//新增的第一页
ddlPageNumber.Items.Add(new ListItem(Convert.ToString(ddlListItemCount + 1), sPageInfo));
for (int i = 2; i <= iPageCount; i++)
{
//""
iRowNum = (i-1) * iPageSize - 1;
listitem = listitems[iRowNum];
sPageInfo = "Paged=TRUE&p_FSObjType=" + listitem["FSObjType"].ToString() + "&p_Title=" + listitem["Title"].ToString() + "&p_ID=" + listitem["ID"].ToString();
ddlPageNumber.Items.Add(new ListItem(Convert.ToString(ddlListItemCount + i), sPageInfo));
}
//显示更多...
if (iPageCount == iPages && listitems.ListItemCollectionPosition != null)
{
ddlPageNumber.Items.Add(new ListItem("more", listitems.ListItemCollectionPosition.PagingInfo));
}
//移除多余的行...
if (iRowsCount > iPageSize)
{
for (int i = iRowsCount - 1; i >= iPageSize; i--)
{
dtResults.Rows.RemoveAt(i);
}
}
}
}
GridView1.DataSource = dtResults;
GridView1.DataBind();
}
}