asp.net 分页控件

好久不用webform了,这次做的项目又要使用webform(微软推荐使用的是mvc),闲着没事儿干了的时候写个分页控件出来,废话不多说直接上代码

[
        DefaultEvent("Paging"),
        DefaultProperty("Text"),
        AspNetHostingPermission(SecurityAction.Demand,
            Level = AspNetHostingPermissionLevel.Minimal),
        AspNetHostingPermission(SecurityAction.InheritanceDemand,
            Level = AspNetHostingPermissionLevel.Minimal),
        ToolboxData("<{0}:Pager runat=\"server\"></{0}:Pager>")
    ]
    public class Pager : CompositeControl
    {
        private LinkButton _lbtnPrevious;
        private LinkButton _lbtnNext;
        private DropDownList _ddlPageItem;

        private static readonly object EventPagingKey =
            new object();

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                string s = (string) ViewState["Text"];
                return (s ?? string.Empty);
            }

            set { ViewState["Text"] = value; }
        }

        [Category("Appearance")]
        [DefaultValue(10)]
        [Localizable(true)]
        public int PageSize
        {
            get
            {
                string s = (string) ViewState["PageSize"];
                int pageSize = 0;
                int.TryParse(s, out pageSize);
                return pageSize;
            }
            set { ViewState["PageSize"] = value.ToString(); }
        }

        [Category("Appearance")]
        [DefaultValue(1)]
        [Localizable(true)]
        public int CurrentPageIndex
        {
            get
            {
                string s = (string) ViewState["CurrentPageIndex"];
                int currentPageIndex = 0;
                int.TryParse(s, out currentPageIndex);
                if (currentPageIndex < 1)
                    currentPageIndex = 1;
                return currentPageIndex;
            }
            set { ViewState["CurrentPageIndex"] = value.ToString(); }
        }

        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        public int RecordCount
        {
            get
            {
                string s = (string) ViewState["RecordCount"];
                int recordCount = 0;
                int.TryParse(s, out recordCount);
                return recordCount;
            }
            set { ViewState["RecordCount"] = value.ToString(); }
        }

        [Category("Appearance")]
        [DefaultValue(0)]
        [Localizable(true)]
        public int PageCount
        {
            get
            {
                string s = (string) ViewState["PageCount"];
                int pageCount = 0;
                int.TryParse(s, out pageCount);
                return pageCount;
            }
            private set { ViewState["PageCount"] = value.ToString(); }
        }

        private void BuildChildControls()
        {
            if (PageSize > 0)
            {
                PageCount = RecordCount / PageSize;
                if (RecordCount % PageSize != 0)
                    PageCount++;
            }

            _lbtnPrevious = new LinkButton { Text = "上一页", ID = "lbtnPrevious", Enabled = (CurrentPageIndex > 1) };
            _lbtnPrevious.Click += _lbtnPrevious_Click;
            this.Controls.Add(_lbtnPrevious);

            _ddlPageItem = new DropDownList { ID = "ddlPageItem", AutoPostBack = true };
            _ddlPageItem.SelectedIndexChanged += _ddlPageItem_SelectedIndexChanged;
            for (var i = 1; i <= PageCount; i++)
            {
                _ddlPageItem.Items.Add(new ListItem(string.Format("第{0}页", i), i.ToString()));
            }
            this.Controls.Add(_ddlPageItem);

            _lbtnNext = new LinkButton { Text = "下一页", ID = "lbtnNext", Enabled = (CurrentPageIndex < PageCount) };
            _lbtnNext.Click += _lbtnNext_Click;
            this.Controls.Add(_lbtnNext);
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            ClearChildControlState();
            BuildChildControls();
        }

        private void _ddlPageItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageIndex;
            int.TryParse(_ddlPageItem.SelectedItem.Value, out pageIndex);
            CurrentPageIndex = pageIndex;
            OnPaging(e);
        }

        public event EventHandler Paging
        {
            add { this.Events.AddHandler(EventPagingKey, value); }
            remove { this.Events.RemoveHandler(EventPagingKey, value); }
        }

        protected virtual void OnPaging(EventArgs e)
        {
            var submitHandler = (EventHandler) Events[EventPagingKey];
            if (submitHandler != null)
            {
                submitHandler(this, e);
            }

            _lbtnPrevious.Enabled = CurrentPageIndex > 1;
            _lbtnNext.Enabled = CurrentPageIndex < PageCount;

            _ddlPageItem.SelectedValue = CurrentPageIndex.ToString();
        }

        private void _lbtnPrevious_Click(object sender, EventArgs e)
        {
            if (CurrentPageIndex > 1)
            {
                CurrentPageIndex -= 1;
                OnPaging(e);
            }
            else
            {
                _lbtnPrevious.Enabled = false;
            }
        }

        private void _lbtnNext_Click(object sender, EventArgs e)
        {
            if (CurrentPageIndex < PageCount)
            {
                CurrentPageIndex += 1;
                OnPaging(e);
            }
            else
            {
                _lbtnNext.Enabled = false;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }

至于怎么用我想广大同行们应该比我更清楚,欢迎改进

时间: 2024-11-09 22:33:47

asp.net 分页控件的相关文章

asp.net分页控件使用详解【附实例下载】

本篇文章主要对asp.net创建事务的方法进行实例介绍,具有很好的参考价值,需要的朋友一起来看下吧 一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer"

asp.net分页控件CSS

.aspx代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample3_DataPager.aspx.cs" Inherits="Sample_03_DataPager" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

【asp.net爬虫】asp.NET分页控件抓取第n页数据 javascript:__doPostBack

最近在模拟HTTP请求抓取数据,但是服务器是asp.net开发的 分页控件代码 <tr> <td align="left">共&nbsp210&nbsp条记录&nbsp--&nbsp第&nbsp2&nbsp页&nbsp--&nbsp共&nbsp3&nbsp页</td><td align="right"><a id="Orac

asp.net分页控件库

AspNetPager分页控件 AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的详细代码: 1.首先到www.webdiyer.com下载最新的AspNetPager.dll,直接在vs2005中添加引用即可. 2.在页面上注册控件,引入该控件,当然,需要在页面中使用一个数据载体,我这里使用的是repeater控件. <%=AspNetPager Namespace"&q

ASP.NET分页控件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageApart.ascx.cs" Inherits="WebApplication2.PageApart" %> <asp:LinkButton ID="LinkButton1" runat="server" onclick="Link

对自写的Asp.Net分页控件的应用方式(异步无刷新分页)

前台代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <link href="css/table.css&quo

Asp.Net数据控件引用AspNetPager.dll分页

今天与大家分享一下"Asp.Net数据控件引用AspNetPager.dll分页"首先声明以下几点: 1.也许讲解有点初级,希望高手不要"喷"我,因为我知道并不是每一个人都是高手,我也怕高手们说我装13,小生不才: 2.如有什么不对的地方,还希望大家指出,一定虚心学习: 3.本文属于作者原创,尊重他人劳动成果,转载请注明作者,谢谢. 下面开讲: 第一步:首先是下载一个AspNetPager.dll 下载地址 http://files.cnblogs.com/tand

asp.net 分页-自己写分页控件

去年就发表过asp.net 分页-利用后台直接生成html分页 ,那种方法只是单纯的实现了分页,基本不能使用,那时就想写个自己的分页控件,无奈能力有限.最近有点时间了,就自己做出了这个分页控件.我承认,这个控件参考了别人的,但是其实里面的原理都相同,差异只是展现方式而已. 去年就在做一个自己的后台系统,刚开始用的asp.net,做了一部分就没有做了,因为缺少权限控制类别.后面转为用asp.net mvc4做,也只是做了一部分,觉得不太方便,也许是我不太会用.再用ajax+ashx做了一部分,虽然

ASP.NET Repeater控件实现简单分页

早上,有看MSDN,看到了 PagedDataSource 类 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.pageddatasource(v=vs.110).aspx 想起以前Insus.NET只对Gridview和DataList控件进行自定义分页.<GridView和DataList分页组件与用户控件>http://www.cnblogs.com/insus/archive/2009/03/19/14