sharepoint 学习笔记汇总
http://blog.csdn.net/qq873113580/article/details/20390149
刚学的时候在网上搜索sharepoint的分页,基本就找到2条代码,看不懂,还好有大神求救。
就把大神的代码贴上来了,直接调用分页,分享给大家
#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)
{
try
{
//查询字段
//string viewFields = string.Empty;
//查询
SPQuery query = new SPQuery();
query.Query = strWhere;
查询的字段
//if (!string.IsNullOrEmpty(fKeyWords.ToLower()))
//{
// query.ViewFields = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + fKeyWords + "</Value></Eq></Where>";
//}
//总共记录数
recourdCount = list.GetItems(query).Count;
//需要重新实例对象
query = new SPQuery();
//设置每页大小
query.RowLimit = (uint)pageSize;
query.Query = strWhere;
// query.ViewFields = viewFields;
//分页信息
string pageinfo = GetListID(list, pageSize, curPage, strWhere);
query.ListItemCollectionPosition = new SPListItemCollectionPosition(pageinfo);
SPListItemCollection m_objListItemColl = list.GetItems(query);
return m_objListItemColl;
}
catch
{
recourdCount = 0;
return null;
}
}
#endregion
#region 返回分页的信息
/// <summary>
/// 返回分页的信息
/// </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, int pageSize, int curPage, string strWhere)
{
try
{
SPQuery query = new SPQuery();
//设置查询条件
query.Query = strWhere;
//查询的字段
//query.ViewFields = viewFields;
//条数限制
query.RowLimit = (uint)((curPage - 1) * pageSize);
//查询
SPListItemCollection m_objListItemColl = list.GetItems(query);
//得到分页信息
string strInfo = m_objListItemColl.ListItemCollectionPosition.PagingInfo;
return strInfo;
}
catch
{
return string.Empty;
}
}
#endregion
#region//构建查询字段
/// <summary>
/// 构建查询字段
/// </summary>
/// <param name="fieldNames">字段</param>
/// <returns>返回构造好的查询字段</returns>
//public static 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
#endregion
--------------------------------------使用案例
前台
<asp:HiddenField ID="hidAllCount" runat="server"/>
<asp:HiddenField ID="hidCurrentPageIndex" runat="server" Value="1" />
<!--分页-->
<div style="overflow-x: auto;">
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
<div style="height: 40px; text-align: center; margin-top: 13px; vertical-align: middle">
<asp:Label ID="labPageMessage" runat="server" Text="" Style="margin-right: 10px;"></asp:Label>
<asp:LinkButton ID="lbtnFrist" runat="server" OnClick="lbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtnUp" runat="server" OnClick="lbtnUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnLastPage" runat="server" OnClick="lbtnLastPage_Click">末页</asp:LinkButton>
<asp:TextBox ID="txtPageNum" runat="server" Style="width: 40px; height: 15px;"></asp:TextBox>
<asp:ImageButton ID="imgBtnGo" runat="server" ImageUrl="../images/img/go.png" OnClick="imgBtnGo_Click"
Style="height: 18px; width: 18px; vertical-align: middle; cursor: pointer;" />
</div>
</div>
<!--分页-->
后台
//返回的集合数量
private int AllCount { get { return Convert.ToInt32(this.hidAllCount.Value); } set { this.hidAllCount.Value = value.ToString(); } }
/// <summary>
/// 当前页
/// </summary>
public int CurrentPageIndex { get { return Convert.ToInt32(this.hidCurrentPageIndex.Value); } set { this.hidCurrentPageIndex.Value = value.ToString(); } }
/// <summary>
/// 页大小
/// </summary>
public int PageSize { get { return 10; } }
int count = 0;
SPListItemCollection SPListItemCollName = Common.GetPageList(splist, this.PageSize, this.CurrentPageIndex, out count, StaAndTypeSearchName(strDrpStatus, StrDrpType));
this.AllCount = count;
SetPageText(this.CurrentPageIndex, count);
#region 分页
//首页
protected void lbtnFrist_Click(object sender, EventArgs e)
{
int page = (int)Math.Ceiling((this.AllCount * 1.0) / (this.PageSize * 1.0));
if (page >= 1)
{
this.CurrentPageIndex = 1;
LoadEmployeeWeeklyReportData();
}
}
//末页
protected void lbtnLastPage_Click(object sender, EventArgs e)
{
int page = (int)Math.Ceiling((this.AllCount * 1.0) / (this.PageSize * 1.0));
if (page >= 1)
{
this.CurrentPageIndex = page;
LoadEmployeeWeeklyReportData();
}
}
//上一页
protected void lbtnUp_Click(object sender, EventArgs e)
{
int page = (int)Math.Ceiling((this.AllCount * 1.0) / (this.PageSize * 1.0));
if (this.CurrentPageIndex != 1 && page != 0)
{
this.CurrentPageIndex = this.CurrentPageIndex - 1;
LoadEmployeeWeeklyReportData();
}
else
{
this.CurrentPageIndex = 1;
LoadEmployeeWeeklyReportData();
}
}
//下一页
protected void lbtnNext_Click(object sender, EventArgs e)
{
int page = (int)Math.Ceiling((this.AllCount * 1.0) / (this.PageSize * 1.0));
if (page != this.CurrentPageIndex && page != 0)
{
this.CurrentPageIndex = this.CurrentPageIndex + 1;
LoadEmployeeWeeklyReportData();
}
else {
if (page == 0)
{
this.CurrentPageIndex = 1;
}
else {
this.CurrentPageIndex = page;
} LoadEmployeeWeeklyReportData();
}
}
#region 设置分页显示信息
private void SetPageText(int currentPage, int pageCount)
{
pageCount = (int)Math.Ceiling((pageCount * 1.0) / (this.PageSize * 1.0));
this.labPageMessage.Text = "当前第" + currentPage + "页,共" + pageCount + "页";
}
#endregion
protected void imgBtnGo_Click(object sender, ImageClickEventArgs e)
{
Regex reg = new Regex(@"^\d+$");
if (reg.IsMatch(this.txtPageNum.Text.Trim()) == false)
{
LoadEmployeeWeeklyReportData();
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('请输入正整数。');</script>");
return;
}
int page = (int)Math.Ceiling((this.AllCount * 1.0) / (this.PageSize * 1.0));
if (Convert.ToInt32(this.txtPageNum.Text) <= 0)
{
LoadEmployeeWeeklyReportData();
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('输入的页数不能等于0。');</script>");
return;
}
if (Convert.ToInt32(this.txtPageNum.Text) > page)
{
LoadEmployeeWeeklyReportData();
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('输入的页数不能超过总页数。');</script>");
return;
}
this.CurrentPageIndex = Convert.ToInt32(this.txtPageNum.Text);
LoadEmployeeWeeklyReportData();
}
#endregion