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

[经验分享] SharePoint GridView的使用,DataSource的实现

[复制链接]

尚未签到

发表于 2015-9-28 13:16:04 | 显示全部楼层 |阅读模式
  SharePoint GridView使用可以通过两种方式来使用:
1. 直接给SPGridVew的DataSource属性赋值一个DataTable, 之后调用BindData方法。缺点是只使用SPGridView来显示数据,而排序,分页,过滤和其他的功能都不可用。
2. 通过SPGridView的DataSourceId绑定一个DataSource控件,之后就可以通过这个DataSource控件实现SPGridView的排序,分页,过滤等功能。
  关于SPGridView的菜单项,排序分页和过滤的实现可以参考:http://www.cnblogs.com/ericfine/archive/2008/10/22/1316431.html
http://blog.iyunv.com/ericfine/archive/2008/10/23/3130387.aspx
  然而我的实现方式并和他一样,在上面链接的文章当中,SPGridView的排序可以使用,过滤也只能过滤一个条件,同时还要一个缺点,如果要显示的数据有上千甚至上万条的数据时,SPGridView还是将全部的数据导入到DataTable中,结果却只显示其中的10(SPGridView的PageSize)条,这样不但速度效率会非常的慢。
  如下是我的实现方式:使用Microsoft.SharePoint.WebControlsDataTableDataSourceView和System.Web.UI.DataSourceControl控件。
WebControlsDataTableDataSourceView是SharePoint实现的一个DataSourceView的派生类。它的主要方法有

DSC0000.gif DSC0001.gif Code
IEnumerable Select(DataSourceSelectArguments selectArguments)  我就不多说了,贴代码给大家直接看吧:
  页面:

Code
<Mine:MyDataSource runat="server" ID="DataSource" ViewName="FilterJob" ></Mine:MyDataSource>
                        <SharePoint:SPGridView PagerSettings-Visible="true" AllowPaging="true" ID="ViewList" runat="server" AllowFiltering="true" FilteredDataSourcePropertyFormat="{1} = '{0}'" FilteredDataSourcePropertyName="FilterExpression"
                         FilterDataFields=",JobId,PlanName,StartTime,EndTime,Status,Progress" DataSourceID="DataSource" AutoGenerateColumns="False" AllowSorting="True" BorderStyle="None" CssClass="ms-listviewtable" GridLines="None"  Width="100%">   
                            <SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
                            <AlternatingRowStyle CssClass="ms-alternating" />
          </SharePoint:SPGridView>  
由于为了实现国际化,Columns的添加在后台,我就不介绍了,上面的那两个链接。
  自定义DataSource的实现:
  

Code
DSC0002.gif [ToolboxData("<{0}:MyDataSource runat=server></{0}:MyDataSource>")]
    public class MyDataSource : DataSourceControl
    DSC0003.gif {
DSC0004.gif         private ACDataSourceView mView;
        private string mViewName;
DSC0005.gif DSC0006.gif         private static string[] mViewNames = new string[] { "FrontEnd", "ScanJob", "ScanReport", "FilterJob", "FilterReport", "ManualScan", "ContentFilter", "RealTimeReport" ,"Test"};

        // Methods
        protected virtual ACDataSourceView CreateView()
        {
            int num = -1;
            if (!String.IsNullOrEmpty(this.ViewName) && (num = GetViewNumber()) != -1)
            {
                switch (num)
                {
                    case 0:
                        return new ACFrontEndDataSourceView(this, this.ViewName, this.Context);
                    case 1:
                        return new ACJobDataSourceView(this, this.ViewName, this.Context);
                    case 2:
                        return new ACScanReportDataSourceView(this, this.ViewName, this.Context);
                    case 3:
                        return new ACJobDataSourceView(this, this.ViewName, this.Context, true);
                    case 4:
                        return new ACFilterReportDataSourceView(this, this.ViewName, this.Context);
                    case 5:
                        return new ACPlanListView(this, this.ViewName, this.Context, false);
                    case 6:
                        return new ACPlanListView(this, this.ViewName, this.Context, true);
                    case 7:
                        return new ACScanReportDataSourceView(this, this.ViewName, this.Context);
                    case 8:
                        return new ACTestDataSourceView(this, this.ViewName, this.Context);
                    default:
                        break;
DSC0007.gif                 }
            }
            return null;
        }

        private int GetViewNumber()
        {
            for (int i = 0; i < mViewNames.Length; i++)
            {
                if (mViewNames == mViewName)
                {
                    return i;
                }
            }
            return -1;
        }

        protected override DataSourceView GetView(string viewName)
        {
            return this.View;
        }

        protected override ICollection GetViewNames()
        {
            return mViewNames;
        }

        private void LoadCompleteEventHandler(object sender, EventArgs e)
        {
            this.SelectParameters.UpdateValues(this.Context, this);
        }

        protected override void LoadViewState(object savedState)
        {
            Pair pair = (Pair)savedState;
            if (savedState != null)
            {
                base.LoadViewState(pair.First);
                if (pair.Second != null)
                {
                    this.View.LoadViewState(pair.Second);
                }
            }
            else
            {
                base.LoadViewState(null);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Page.LoadComplete += new EventHandler(this.LoadCompleteEventHandler);
        }

        protected override object SaveViewState()
        {
            Pair pair = new Pair();
            pair.First = base.SaveViewState();
            if (this.mView != null)
            {
                pair.Second = this.mView.SaveViewState();
            }
            if (pair.First != null)
            {
                return pair;
            }
            if (pair.Second != null)
            {
                return pair;
            }
            return null;
        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
            if (this.mView != null)
            {
                this.mView.TrackViewState();
            }
        }

        // Properties
        [MergableProperty(false), DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty)]
        public ParameterCollection SelectParameters
        {
            get
            {
                return this.View.SelectParameters;
            }
        }

        protected ACDataSourceView View
        {
            get
            {
                if (this.mView == null)
                {
                    this.mView = this.CreateView();
                    if (base.IsTrackingViewState)
                    {
                        this.mView.TrackViewState();
                    }
                }
                return this.mView;
            }
        }

        public string ViewName
        {
            get
            {
                return this.mViewName;
            }
            set
            {
                mViewName = value;
            }
        }

        public string FilterExpression
        {
            get
            {
                return this.View.FilterExpression;
            }
            set
            {
                this.View.FilterExpression = value;
            }
        }

        public bool IsFilter
        {
            get
            {
                return this.View.IsFilter;
            }
            set
            {
                this.View.IsFilter = value;
            }
DSC0008.gif         }  DataSourceView的实现在后面的文章再介绍。

运维网声明 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-120032-1-1.html 上篇帖子: SharePoint最简母版页 下篇帖子: 利用Microsoft.SharePoint.Administration 来管理WSS(sharepoint)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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