SharePoint:SPGridview控件分页
2010-05-06 15:19 Leason Li 阅读(1701) 评论(7) 编辑 收藏 举报MOSS里面的SPGridview控件功能还是比较强大的,可以实现丰富的分页,分组,排序,筛选功能,但更为强大的是针对每一项数据,可以实现下拉菜单的功能。今天用到了它简单的分页功能,虽然简单但是由于他的特殊性(和gridview分页的实现方式不一样),这里记录下供大家参考。
首先SPGridview是继承自Gridview的一个控件,拥有Gridview的所有功能(但实现方式不一样),并对其扩展。
这些控件的数据源非常丰富,可以是数据库,datatable,xml文件,或者是ObjectDataSource对象,这里分别讲下简单的SPGridview和Gridview分页实现方式,当然也可以手动的去写分页(结合存储过程每页需要多少条数据,取多少条数据,这种方法一般针对性能要求很高的大型数据的分页查询)。
Gridview分页实现方式很简单,步骤如下:
1:设置属性
2:实现事件
{
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataBind();
}
当然还得指定数据源了

{
this.GridView1.DataSource = GetEnterpriseInfo();
this.GridView1.DataBind();
}
public DataTable GetEnterpriseInfo()
{
DataTable enterpriseInfo = new DataTable();
// SPSite site = SPContext.Current.Site;
SPSite site = new SPSite("http://moss:10000");
SPList list = site.RootWeb.Lists["员工通讯录"];
if (list == null)
{
return null;
}
SPListItemCollection items;
SPQuery query = new SPQuery();
string queryStr = @"
<Where>
<IsNotNull>
<FieldRef Name='SerialNumber' />
</IsNotNull>
</Where>
";
query.Query = queryStr;
items = list.GetItems(query);
enterpriseInfo = items.GetDataTable();
return enterpriseInfo;
}
而SPGridview的分页实现不会那么智能,需要手动实现的部分更多,而且一定需要在Sharepoint的环境下运行,否则分页的效果不会出现
1:设置属性
2:引用SPGridViewPager控件
</CC:SPGridViewPager>
前台代码如下:

CodeFile="DXMobileNumber.aspx.cs" Inherits="DXMobileNumber" Title="短信平台-员工通讯录" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Namespace="Microsoft.SharePoint.WebControls" TagPrefix="CC" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="Server">
<style type="text/css">
.table_stroke
{
border-collapse: collapse;
}
.form-title
{
border: 1px solid #969696;
background-color: #c51100;
height: 25px;
}
.form-title_font
{
font-size: medium;
font-family: "微软雅黑";
font-weight: bold;
color: #ffffff;
}
.form-title_font1
{
font-size: medium;
font-family: "微软雅黑";
font-weight: bold;
color: #f07400;
}
body
{
margin-top: 36px;
}
.form-title2
{
background-color: #f99131;
height: 20px;
font-family: "宋体";
font-size: 12px;
font-weight: bold;
color: #333333;
border-top: 1px solid #969696;
border-right: 1px solid #969696;
border-left: 1px solid #969696;
}
.form-title3
{
border: 1px solid #969696;
background: #ffc675;
border: 1px solid #808080;
height: 28px;
font-family: "微软雅黑";
font-size: 14px;
font-weight: bold;
color: #333333;
text-align: center;
}
.form-label
{
background: #ffffff;
border: 1px solid #808080;
height: 25px;
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
}
.form-body
{
background: #fcebb7;
border: 1px solid #808080;
height: 25px;
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
}
.table_stroke1
{
border-collapse: collapse;
}
.table_stroke2
{
border: 1px solid #ffffff;
}
.table_bluebg3
{
background: #f9f9f9;
}
.form-button
{
font-size: 12px;
font-family: "宋体";
color: #000000;
width: 80px;
}
.form-input
{
font-size: 12px;
font-family: "宋体";
color: #000000;
border: 1px solid #c8c8c8;
background-image: url(blank.gif);
background-repeat: no-repeat;
background-position: right top;
height: 20px;
}
.form-input1
{
font-size: 12px;
font-family: "宋体";
color: #000000;
border: 1px solid #c8c8c8;
height: 20px;
}
.form-select
{
font-size: 12px;
font-family: "宋体";
color: #000000;
border: 1px solid #c8c8c8;
width: 140px;
height: 20px;
}
.form-select1
{
font-size: 12px;
font-family: "微软雅黑";
color: #000000;
border: 1px solid #c8c8c8;
width: 100px;
height: 20px;
}
</style>
<table width="978" border="0" align="center" cellpadding="0" cellspacing="0" class="table_stroke">
<tr>
<td align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="59%" align="right" class="form-title_font1">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<div id="PrintContent">
<style type="text/css">
.GridViewStyle a
{
color: #FFFFFF;
}
.form-title_font
{
font-size: medium;
font-family: "微软雅黑";
font-weight: bold;
color: #ffffff;
}
.GridViewHeaderStyle th
{
border-left: 1px solid #EBE9ED;
border-right: 1px solid #EBE9ED;
}
.GridViewHeaderStyle
{
background-color: #5D7B9D;
font-weight: bold;
color: White;
}
.GridViewFooterStyle
{
background-color: #5D7B9D;
font-weight: bold;
color: White;
}
.GridViewRowStyle
{
background: #ffffff;
border: 1px solid #808080;
height: 15px;
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
}
.GridViewAlternatingRowStyle
{
background: #fcebb7;
border: 1px solid #808080;
height: 15px;
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
}
.GridViewRowStyle td, .GridViewAlternatingRowStyle td
{
border: 1px solid #808080;
}
.GridViewSelectedRowStyle
{
background-color: #E2DED6;
font-weight: bold;
color: #333333;
}
.GridViewPagerStyle
{
background-color: #284775;
color: #FFFFFF;
}
.GridViewPagerStyle table /* to center the paging links*/
{
margin: 0 auto 0 auto;
}
.ms-vh2 TABLE.ms-selectedtitle
{
border-top-style: none;
border-right-style: none;
border-left-style: none;
background-color: #ffffff;
border-bottom-style: none;
text-align: center;
}
.ms-vh2 TABLE.ms-unselectedtitle
{
height: 21px;
text-align: center;
}
.ms-vh2-nofilter A
{
font-family: "微软雅黑";
font-size: 14px;
font-weight: bold;
color: #333333;
text-align: center;
}
.ms-vh2
{
border: 1px solid #969696;
background: #ffc675;
border: 1px solid #808080;
height: 28px;
text-align: center;
}
.ms-vh2-nofilter
{
border: 1px solid #969696;
background: #ffc675;
border: 1px solid #808080;
height: 28px;
text-align: center;
}
.ms-vh2 A
{
text-align: center;
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
}
.ms-vh2 A:hover
{
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
text-decoration: underline;
}
.ms-vh2-nofilter A:hover
{
font-family: "微软雅黑";
font-size: 14px;
color: #333333;
text-align: center;
text-decoration: underline;
}
.form-title2
{
background-color: #f99131;
height: 20px;
font-family: "宋体";
font-size: 12px;
font-weight: bold;
color: #333333;
border-top: 1px solid #969696;
border-right: 1px solid #969696;
border-left: 1px solid #969696;
}
</style>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" class="form-title2">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="59%" align="center" class="form-title_font">
<asp:Label ID="Year" runat="server" Text="员工通讯录基本信息"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" class="table_bluebg3">
<table width="100%" border="0" cellpadding="2" cellspacing="0" class="table_stroke1">
<tr>
<td>
<CC:SPGridView ID="SPGridView1" GridLines="Both" runat="server" AutoGenerateColumns="False"
AllowPaging="true" PageSize="1">
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle ForeColor="red" CssClass="GridViewHeaderStyle" />
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"><%# Container.DataItemIndex + 1%></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<a style="color: Red">选择</a>
</HeaderTemplate>
<ItemTemplate>
<input id="checkSelect" runat="server" type="checkbox" value='<%# Eval("SerialNumber") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="姓名" />
<asp:BoundField DataField="Company" HeaderText="公司名称" />
<asp:BoundField DataField="Rank" HeaderText="所属党支部" />
<asp:BoundField DataField="SerialNumber" HeaderText="手机号码" />
</Columns>
</CC:SPGridView>
<CC:SPGridViewPager ID="sppager" runat="server" GridViewId="SPGridView1">
</CC:SPGridViewPager>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td style="height: 25px;">
</td>
</tr>
<tr>
<td align="center" style="font-size: 16px; color: #5b5a5a; line-height: 20px; font-family: 仿宋_GB2312">
<asp:Button ID="btnSelection" runat="server" Text="评选" Width="83px" />
<asp:Button ID="btnCancle" runat="server" Text="返回" Width="83px" />
</td>
</tr>
</table>
</asp:Content>
3:实现ObjectDataSource数据源

odsDataSource.ID = "ExampleSource";
odsDataSource.TypeName = this.GetType().FullName + "," + this.GetType().Assembly.FullName;
odsDataSource.SelectMethod = "GetEnterpriseInfo";
Controls.Add(odsDataSource);
后台代码如下:

using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using Microsoft.SharePoint.WebControls;
public partial class DXMobileNumber : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ObjectDataSource odsDataSource = new ObjectDataSource();
odsDataSource.ID = "ExampleSource";
odsDataSource.TypeName = this.GetType().FullName + "," + this.GetType().Assembly.FullName;
odsDataSource.SelectMethod = "GetEnterpriseInfo";
Controls.Add(odsDataSource);
this.SPGridView1.DataSourceID = "ExampleSource";
this.SPGridView1.DataBind();
}
public DataTable GetEnterpriseInfo()
{
DataTable enterpriseInfo = new DataTable();
// SPSite site = SPContext.Current.Site;
SPSite site = new SPSite("http://moss:10000");
SPList list = site.RootWeb.Lists["员工通讯录"];
if (list == null)
{
return null;
}
SPListItemCollection items;
SPQuery query = new SPQuery();
string queryStr = @"
<Where>
<IsNotNull>
<FieldRef Name='SerialNumber' />
</IsNotNull>
</Where>
";
query.Query = queryStr;
items = list.GetItems(query);
enterpriseInfo = items.GetDataTable();
return enterpriseInfo;
}
}
4:最后一步一定要记住:需要把该ASP.NET页面放到_layouts目录(做过MOSS开发的肯定对这个目录不会陌生吧)。最后给大家放个截图:
总结:MOSS里面的页面类型主要有两种,一种是Application page(存在_layouts目录),另外一种是内容页面类型(存在数据库中,在本地磁盘找不到实际的物理文件,可用sharepoint designer从数据库中映射出来查看并修改)。往往我们需要开发一些页面放到sharepoint平台中使用,而这个时候使用到的就是Application page,如上我们开发的一个从列表中检索数据到另外一个页面上,就是创建了一个Application页面,必须放在sharepoint环境中才能使用,因为里面使用到了一些控件,如spgridview,DateTimeControl控件等,如果要使用这些控件的某些特定功能,必须是放在sharepoint环境中,即_layouts目录下。
作者:leening_li
出处:http://www.cnblogs.com/leening/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。