惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - 阿福

TypeScript forms authentication failed the ticket supplied was invalid错误 (Windows Server 2008 + IIS 7.5) jQuery Mobile 1.1.1 RC1发布 HashSet<T> vs List<T> 不要用把无序GUID既作为主键又作为聚集索引 WCF Data Services 5.0 RTM发布 EF Power Tools Beta 2发布 Entity Framework 5性能方面的注意事项 jQuery Mobile 1.1.0 RC2发布 源码+幻灯片:学习HTML5/jQuery/ASP.NET MVC/EF Code First的绝佳资源Account at a Glance项目 使用Autofac在ASP.NET Web API上实现依赖注入 在Windows Azure上开发ASP.NET程序与在Windows Sever上有何不同 充分利用缓存来提高网站性能 ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor)全部开源,并接受来自社区的贡献(contributions) EF5 beta2通过NuGet发布 Getting Started with HTML5 开发HTML5应用你需要了解的 WCF入门资源 jquery history plugin, url hash Run Tasks in an ASP.NET Application Domain ASP.NET 2.0: 生成Excel报表 VS 2008 hot-fix终于出来了 Images; How to create an HTTP handler to dynamically resize images and change quality. Tip/Trick ASP.NET 2.0: DropDownList DataBind ASP.NET 2.0: Forms Authentication Across domains Ajax类库、框架、工具包完全列表 怀旧啊 Search Box ASP.NET 2.0: Site Maps Checking All CheckBoxes in a GridView Efficient Data Paging and Sorting with ASP.NET 2.0 and SQL 2005 JavaScript Calendar Cool MSDN ASP.NET 2.0 GridView Control Article Do I have to cry for you Google无法访问 扯淡 狗*! Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers Great New Advanced Article on ASP.NET 2.0 Master Pages ASP.NET 2.0:通过SqlDataSource绑定数据到普通控件 通过客户端扩展实现固定GridView表头功能 不使用ISAPI或IIS wildcard实现不带扩展名URL的转向 VS 2005 Web Application Project & Atlas Control Toolkit & CSS Control Adapter ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题转换 How to use virtual path providers to dynamically load and compile content from virtual paths in 
ASP.NET 2.0: Add build-in paging feature to repeater/为repeater添加内置分页功能
阿福 · 2010-01-01 · via 博客园 - 阿福

Keywords: paging with repeater control,dataource control, sqldatasource, repeater control 分页

Introduction

Using repeater to list your data has many benefits, it's light, hight-perfomance, and gives you more control to the layout. Especially when the data needs to be nested. It's difficult to control the item layout when using gridview. When used with datasource control, repeater can be codeless too, just like the gridview. But the drawback of the repeater is that it's lack of  build in paing and sorting feature when bind to datasource. That means you bind a repeater with a data sqlsource, this can be no code at all (I'm meaning the code in the codebehind file). But if you want to page the data, you have to throw away the sqldatasource and do the databind as the way you do in asp.net 1.x. Surely, this could make you very uncomfortalbe. In this article, I discuss how to add the paing feature to the repeater and does not lose the convenience of datasource databinding.

I gave out the source code first, you can download it here

public class Repeater : System.Web.UI.WebControls.Repeater

    {

        public Repeater()

        {

            this._pageCount = -1;

            this._recordCount = -1;

        }
        #region pager properties

        [DefaultValue(false)]

        public virtual bool AllowPaging

        {

            get

            {

                object obj2 = this.ViewState["AllowPaging"];

                if (obj2 != null)

                {

                    return (bool)obj2;

                }

                return false;

            }

            set

            {

                bool allowPaging = this.AllowPaging;

                if (value != allowPaging)

                {

                    this.ViewState["AllowPaging"] = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }

        private int _pageCount;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

        public virtual int PageCount

        {

            get

            {

                if (this._pageCount >= 0)

                {

                    return this._pageCount;

                }
                object obj2 = this.ViewState["PageCount"];

                if (obj2 == null)

                {

                    return 0;

                }

                return (int)obj2;

            }

        }
        private int _recordCount;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

        public virtual int RecordCount

        {

            get

            {

                if (this._recordCount >= 0)

                {

                    return this._recordCount;

                }

                object obj2 = this.ViewState["RecordCount"];

                if (obj2 == null)

                {

                    return 0;

                }

                return (int)obj2;

            }

        }

        private int _pageIndex;

        [Browsable(true), DefaultValue(0)]

        public virtual int PageIndex

        {

            get

            {

                return this._pageIndex;

            }

            set

            {

                if (value < 0)

                {

                    throw new ArgumentOutOfRangeException("value");

                }

                if (this.PageIndex != value)

                {

                    this._pageIndex = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }
        [DefaultValue(10)]

        public virtual int PageSize

        {

            get

            {

                object obj2 = this.ViewState["PageSize"];

                if (obj2 != null)

                {

                    return (int)obj2;

                }

                return 10;

            }

            set

            {

                if (value < 1)

                {

                    throw new ArgumentOutOfRangeException("value");

                }

                if (this.PageSize != value)

                {

                    this.ViewState["PageSize"] = value;

                    if (base.Initialized)

                    {

                        base.RequiresDataBinding = true;

                    }

                }

            }

        }

        #endregion
        #region additional templates

        private ITemplate _pagerTemplate;

        [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string)null), TemplateContainer(typeof(RepeaterItem))]

        public virtual ITemplate PagerTemplate

        {

            get

            {

                return this._pagerTemplate;

            }

            set

            {

                this._pagerTemplate = value;

            }

        }
        private ITemplate _emptyDataTemplate;

        [Browsable(false), DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty)]

        public virtual ITemplate EmptyDataTemplate

        {

            get

            {

                return this._emptyDataTemplate;

            }

            set

            {

                this._emptyDataTemplate = value;

            }

        }

 
        #endregion
        #region override methods

        protected override IEnumerable GetData()

        {

            IEnumerable data = base.GetData();

            if (data != null)

            {

                PagedDataSource source = new PagedDataSource();

                //source.AllowCustomPaging = false;

                source.DataSource = data;

                source.AllowPaging = this.AllowPaging;

                //source.AllowServerPaging = true ;

                source.CurrentPageIndex = this.PageIndex;

                //source.VirtualCount = 0;

                source.PageSize = this.PageSize;

                this.ViewState["PageCount"] = source.PageCount; ;

                this.ViewState["RecordCount"] = source.DataSourceCount;

                return source;

            }

            return data;

        }

        protected override void CreateControlHierarchy(bool useDataSource)

        {

            base.CreateControlHierarchy(useDataSource);

            if (this._pagerTemplate != null)

            {



                RepeaterItem pager = CreateItem(-1, ListItemType.Pager);

                this._pagerTemplate.InstantiateIn(pager);

                this.Controls.Add(pager);

                pager.DataBind();

            }

        }

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            if (this.Page != null)

            {

                this.Page.RegisterRequiresControlState(this);

            }

        }

        protected override void LoadControlState(object savedState)

        {

            object[] objArray = savedState as object[];

            if (objArray != null)

            {

                base.LoadControlState(objArray[0]);

                if (objArray[1] != null)

                {

                    this._pageIndex = (int)objArray[1];

                }

            }

        }

        protected override object SaveControlState()

        {

            object obj2 = base.SaveControlState();

            if (obj2 != null || this._pageIndex != 0)

            {

                return new object[] { obj2, this._pageIndex };

            }

            return true;

        }

        protected override bool OnBubbleEvent(object sender, EventArgs e)

        {

            bool flag = false;

            
            RepeaterCommandEventArgs args = e as RepeaterCommandEventArgs;

            if (args != null)

            {

                

                if (args.CommandName.Equals(DataControlCommands.PageCommandName, StringComparison.OrdinalIgnoreCase))

                {

                    this.HandlePage(args);

                }

                else

                {

                    this.OnItemCommand(args);

                }

                flag = true;

            }

            return flag;
        }

        #endregion
        #region private methods

        private void HandlePage(RepeaterCommandEventArgs e)

        {

            bool causesValidation = false;

            string validationGroup = string.Empty;

            IButtonControl control = e.CommandSource as IButtonControl;

            if (control != null)

            {

                causesValidation = control.CausesValidation;

                validationGroup = control.ValidationGroup;

            }

            if (causesValidation)

            {

                this.Page.Validate(validationGroup);

            }

            string commondArgument = (string)e.CommandArgument;

            int newPageIndex = this.PageIndex;

            if (commondArgument.Equals(DataControlCommands.NextPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex++;

            }

            else if (commondArgument.Equals(DataControlCommands.PreviousPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex--;

            }

            else if (commondArgument.Equals(DataControlCommands.FirstPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                newPageIndex = 0;

            }

            else if (commondArgument.Equals(DataControlCommands.LastPageCommandArgument, StringComparison.OrdinalIgnoreCase))

            {

                if (base.IsViewStateEnabled)

                {

                    newPageIndex = this.PageCount - 1;

                }

                else

                {

                    newPageIndex = 0x7fffffff;

                }

            }

            else

            {

                newPageIndex = Convert.ToInt32(commondArgument, System.Globalization.CultureInfo.InvariantCulture) - 1;

            }

            if (this.AllowPaging && IsBoundUsingDataSourceID)

            {

                if (newPageIndex < 0) return;

                if (newPageIndex > this.PageCount - 1) return;

                this._pageIndex = newPageIndex;

                base.RequiresDataBinding = true;

            }
        }

        #endregion
    }
How to use:    

   

        AllowPaging="True">

        

            

            



        

        

            

            

            

            

        

    

    

        SelectCommand="SELECT [PurchaseOrderNo], [PurchaseOrderID] FROM [poPurchaseOrder]">