设为首页 收藏本站
查看: 585|回复: 0

[经验分享] SharePoint:SPGridview控件分页

[复制链接]

尚未签到

发表于 2015-9-25 08:13:49 | 显示全部楼层 |阅读模式
  MOSS里面的SPGridview控件功能还是比较强大的,可以实现丰富的分页,分组,排序,筛选功能,但更为强大的是针对每一项数据,可以实现下拉菜单的功能。今天用到了它简单的分页功能,虽然简单但是由于他的特殊性(和gridview分页的实现方式不一样),这里记录下供大家参考。
  首先SPGridview是继承自Gridview的一个控件,拥有Gridview的所有功能(但实现方式不一样),并对其扩展。
  这些控件的数据源非常丰富,可以是数据库,datatable,xml文件,或者是ObjectDataSource对象,这里分别讲下简单的SPGridview和Gridview分页实现方式,当然也可以手动的去写分页(结合存储过程每页需要多少条数据,取多少条数据,这种方法一般针对性能要求很高的大型数据的分页查询)。

  Gridview分页实现方式很简单,步骤如下:
  1:设置属性


AllowPaging="True" PageSize="1"   2:实现事件



   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
    }  当然还得指定数据源了


DSC0000.gif DSC0001.gif 代码

    protected void Page_Load(object sender, EventArgs e)
    {
        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:设置属性


AllowPaging="true" PageSize="1"  2:引用SPGridViewPager控件



<CC:SPGridViewPager ID="sppager" runat="server" GridViewId="SPGridView1">
                                            </CC:SPGridViewPager>  前台代码如下:


代码

<%@ Page Language="C#" MasterPageFile="~/Application.master" AutoEventWireup="true"
    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数据源

  

代码

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


代码

using System;
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开发的肯定对这个目录不会陌生吧)。最后给大家放个截图:
DSC0002.jpg
      总结:MOSS里面的页面类型主要有两种,一种是Application page(存在_layouts目录),另外一种是内容页面类型(存在数据库中,在本地磁盘找不到实际的物理文件,可用sharepoint designer从数据库中映射出来查看并修改)。往往我们需要开发一些页面放到sharepoint平台中使用,而这个时候使用到的就是Application page,如上我们开发的一个从列表中检索数据到另外一个页面上,就是创建了一个Application页面,必须放在sharepoint环境中才能使用,因为里面使用到了一些控件,如spgridview,DateTimeControl控件等,如果要使用这些控件的某些特定功能,必须是放在sharepoint环境中,即_layouts目录下。
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118374-1-1.html 上篇帖子: SharePoint提供的一些javascript函数 下篇帖子: Calling a WCF Service using jQuery in SharePoint
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表