利用ListItemCollectionPosition和AspNetPage分页控件实现,效果图如下:
后台分页代码如下:
#region 私有方法
/// <summary>
/// 数据
/// </summary>
private void BindsData()
{
SPWeb m_objWeb = SPContext.Current.Web;
//
if (m_objWeb != null)
{
int _totalCount =0;
//
//
string listName = "测试列表";
//
SPList m_objList = m_objWeb.Lists[listName];
//
SPListItemCollection itemCollection = GetPageList(listName, this.AspNetPager_View.PageSize,
this.AspNetPager_View.CurrentPageIndex, out _totalCount, "", new string[] { "Title" });
//
this.Repeater1.DataSource = itemCollection;
this.Repeater1.DataBind();
this.AspNetPager_View.RecordCount = _totalCount;
}
}
/// <summary>
/// 根据列表得到首页ID
/// </summary>
/// <param name="listName">列表名称</param>
/// <param name="pageSize"></param>
/// <param name="strWhere">查询条件</param>
/// <param name="ViewField">查询的字段</param>
/// <returns></returns>
private int GetListID(string listName, int pageSize, int curPage, string strWhere, string[] ViewField)
{
try
{
//得到当前站点
SPWeb m_objWeb = SPContext.Current.Web;
//得到列表
SPList m_objList = m_objWeb.Lists[listName];
SPQuery query = new SPQuery();
//设置查询条件
query.Query = strWhere;
//查询的字段
if (ViewField.Length > 0)
{
query.ViewFields = BuildViewFields(ViewField);
}
//条数限制
query.RowLimit = (uint)((curPage - 1) * pageSize);
//查询
SPListItemCollection m_objListItemColl = m_objList.GetItems(query);
//得到分页信息
string strPageInfo = m_objListItemColl.ListItemCollectionPosition.PagingInfo;
string page = strPageInfo.Substring(strPageInfo.LastIndexOf('=') + 1);
return Convert.ToInt32(page);
}
catch
{
return 0;
}
}
/// <summary>
/// 列表分页
/// </summary>
/// <param name="listName">列表名称</param>
/// <param name="pageSize">每页大小</param>
/// <param name="curPage">当前页数</param>
/// <param name="recourdCount">总共记录数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="ViewField">查询的字段</param>
/// <returns></returns>
public SPListItemCollection GetPageList(string listName, int pageSize, int curPage, out int recourdCount, string strWhere,string[] ViewField)
{
try
{
//得到当前站点
SPWeb m_objWeb = SPContext.Current.Web;
//得到列表
SPList m_objList = m_objWeb.Lists[listName];
//查询
SPQuery query = new SPQuery();
query.Query = strWhere;
//查询的字段
if (ViewField.Length>0)
{
query.ViewFields = BuildViewFields(ViewField);
}
//总共记录数
recourdCount = m_objList.GetItems(query).Count;
query = new SPQuery();
//设置每页大小
query.RowLimit = (uint)pageSize;
query.Query = strWhere;
//查询的字段
if (ViewField.Length > 0)
{
query.ViewFields = BuildViewFields(ViewField);
}
//分页信息
string pageinfo = string.Format("Paged=TRUE&p_ID={0}", GetListID(listName, pageSize, curPage, strWhere,ViewField));
query.ListItemCollectionPosition = new SPListItemCollectionPosition(pageinfo);
SPListItemCollection m_objListItemColl = m_objList.GetItems(query);
return m_objListItemColl;
}
catch
{
recourdCount = 0;
return null;
}
}
/// <summary>
/// 构建查询字段
/// </summary>
/// <param name="fieldNames"></param>
/// <returns></returns>
public string BuildViewFields(string[] fieldNames)
{
if (fieldNames ==null|| fieldNames.Length ==0)
return"";
string result ="";
foreach (string fieldName in fieldNames)
{
result = String.Format("{0}<FieldRef Name=\"{1}\" />", result, fieldName);
}
return result;
}
#endregion
页面HTML代码如下:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs"
Inherits="SharePointProject1.VisualWebPart1.VisualWebPart1UserControl" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<style type="text/css">
/*****************AspNetPager分页控件的样式:迅雷样式******************************/
.pages { color: #999; }
.pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
.pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
.pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;}
/*****************AspNetPager分页控件的样式:迅雷样式******************************/
</style>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="0">
<tr>
<td>
标题
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Title")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<webdiyer:AspNetPager ID="AspNetPager_View" runat="server" HorizontalAlign="Center"
NumericButtonCount="3" FirstPageText="[首页]" LastPageText="[尾页]" NextPageText="[后页]"
PrevPageText="[前页]" NumericButtonTextFormatString="[{0}]" TextAfterPageIndexBox="页"
CssClass="pages" CurrentPageButtonClass="cpb" TextBeforePageIndexBox="转第" AlwaysShow="True"
ShowPageIndexBox="Always" ShowCustomInfoSection="Left" CustomInfoHTML="共<font color=red>%RecordCount%</font>条 当前<font color=red>%CurrentPageIndex%</font>/<font color=red>%PageCount%</font>"
OnPageChanged="AspNetPager_View_PageChanged">
</webdiyer:AspNetPager>
经过5000条列表的记录测试,每次翻页加载速度为1秒多点(说明:机器为本人笔记本,虚拟机内存2.5G)
最新修改,在网友的帮助下修改的。可以按照自己实际情况来排序。
#region//分页
#region//根据分页条数进行分页 /// <summary>
/// 列表分页
/// </summary>
/// <param name="listName">列表名称</param>
/// <param name="pageSize">每页大小</param>
/// <param name="curPage">当前页数</param>
/// <param name="recourdCount">总共记录数</param>
/// <param name="strWhere">查询条件</param>
/// <param name="ViewField">查询的字段</param>
/// <returns></returns>
public static SPListItemCollection GetPageList(SPList list, int pageSize, int curPage, out int recourdCount, string strWhere, string[] ViewField)
{
try
{
//查询字段
string viewFields = string.Empty; //查询
SPQuery query = new SPQuery();
query.Query = strWhere;
//查询的字段
if (ViewField.Length > )
{
viewFields = BuildViewFields(ViewField);
}
//总共记录数
recourdCount = list.GetItems(query).Count;
//需要重新实例对象
query = new SPQuery();
//设置每页大小
query.RowLimit = (uint)pageSize;
query.Query = strWhere;
query.ViewFields = viewFields; //分页信息
string pageinfo = GetListID(list, viewFields, pageSize, curPage, strWhere, ViewField);
query.ListItemCollectionPosition = new SPListItemCollectionPosition(pageinfo); SPListItemCollection m_objListItemColl = list.GetItems(query);
return m_objListItemColl;
}
catch
{
recourdCount = ;
return null;
}
} #endregion #region//构建查询字段
/// <summary>
/// 构建查询字段
/// </summary>
/// <param name="fieldNames">字段</param>
/// <returns>返回构造好的查询字段</returns>
public static string BuildViewFields(string[] fieldNames)
{
if (fieldNames ==null|| fieldNames.Length ==)
return"";
string result ="";
foreach (string fieldName in fieldNames)
{
result = String.Format("{0}<FieldRef Name=\"{1}\" />", result, fieldName);
}
return result;
}
#endregion #region//返回分页的ID
/// <summary>
/// 根据列表得到首页ID
/// </summary>
/// <param name="listName">列表名称</param>
/// <param name="pageSize"></param>
/// <param name="strWhere">查询条件</param>
/// <param name="ViewField">查询的字段</param>
/// <returns></returns>
public static string GetListID(SPList list, string viewFields, int pageSize, int curPage, string strWhere, string[] ViewField)
{
try
{
SPQuery query = new SPQuery();
//设置查询条件
query.Query = strWhere;
//查询的字段
query.ViewFields = viewFields;
//条数限制
query.RowLimit = (uint)((curPage - ) * pageSize);
//查询
SPListItemCollection m_objListItemColl = list.GetItems(query);
//得到分页信息
string strPageInfo = m_objListItemColl.ListItemCollectionPosition.PagingInfo;
return strPageInfo;
}
catch
{
return string.Empty;
}
} #endregion #endregion