【开源】QuickPager ASP.NET2.0分页控件V2.0.0.1——分页控件的源码(一) 主体

namespace JYK.Controls
{
    /**//// <summary>
    /// 分页控件
    /// PageGetData.cs 负责提取数据
    /// PageSQL.cs 负责生成SQl语句
    /// PageUI.cs 负责分页控件的页面内容
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:JYKPage runat=server></{0}:JYKPage>")]
    public class JYKPage : WebControl, INamingContainer, IPostBackEventHandler 

    {
        /**//// <summary>
        /// 访问数据库用的实例
        /// </summary>
        private  DataAccessHelp dal = null;

        /**//// <summary>
        /// 生成SQL语句的部分
        /// </summary>
        private PageManage.PageSQL MgrPageSQL = null;

        /**//// <summary>
        /// 提取数据的部分
        /// </summary>
        private PageManage.PageGetData MgrGetData = null;

        /**//// <summary>
        /// 提取数据的部分
        /// </summary>
        private PageManage.PageUI MgrPageUI = null;

        /**//// <summary>
        /// 设置显示数据的控件
        /// </summary>
        public System.Web.UI.WebControls.WebControl ControlGrid;
 
        成员#region 成员
        
        页面显示#region 页面显示
        /**//// <summary>
        /// 上一页 的文字
        /// </summary>
        private string pPrevText = "上一页";   //上一页

        /**//// <summary>
        /// 下一页 的文字
        /// </summary>
        private string pNextText = "下一页";   //下一页

        /**//// <summary>
        /// 首页 的文字
        /// </summary>
        private string pFirstText = "首页";   //首页

        /**//// <summary>
        /// 末页 的文字
        /// </summary>
        private string pLastText = "末页";   //末页
        
        /**//// <summary>
        /// 去指定页号 的文字
        /// </summary>
        private string pGOText = "GO";   //GO
        
        #endregion

        #endregion

        属性#region 属性

        存放分页算法模版的属性#region 存放分页算法模版的属性
        //记录数
        记录相关#region 记录相关
        一页的记录数#region 一页的记录数
        /**//// <summary>
        /// 一页的记录数
        /// </summary>
        [Bindable(true)]
        [Category("记录集")]
        [DefaultValue("")]
        [Localizable(true)]
        public Int32 PageSize
        {
            set { ViewState["PageSize"] = value; }
            get
            {
                //没有设置的话,使用默认值:一页20条记录
                if (ViewState["PageSize"] == null) { return 20; }
                else { return (Int32)ViewState["PageSize"]; }
            }
        }
        #endregion

        符合条件的记录数#region 符合条件的记录数
        /**//// <summary>
        /// 符合条件的记录数
        /// </summary>
        [Bindable(true)]
        [Category("记录集")]
        [DefaultValue("")]
        [Localizable(true)]
        public Int32 PageRecordCount
        {
            set { ViewState["PageRecordCount"] = value; }
            get
            {
                //没有设置的话,返回 0 
                if (ViewState["PageRecordCount"] == null) { return 0; }
                else { return (Int32)ViewState["PageRecordCount"]; }
            }
        }
        #endregion

        页数#region 页数
        /**//// <summary>
        /// 页数
        /// </summary>
        [Bindable(true)]
        [Category("记录集")]
        [DefaultValue("")]
        [Localizable(true)]
        public Int32 PageCount
        {
            set { ViewState["PageCount"] = value; }
            get
            {
                //没有设置的话,返回 0 
                if (ViewState["PageCount"] == null) { return 0; }
                else { return (Int32)ViewState["PageCount"]; }
            }
        }
        #endregion

        想要查看哪一页的数据#region 想要查看哪一页的数据
        /**//// <summary>
        /// 想要查看那一页的数据
        /// </summary>
        [Bindable(true)]
        [Category("记录集")]
        [DefaultValue("")]
        [Localizable(true)]
        public Int32 PageIndex
        {
            set { ViewState["PageIndex"] = value; }
            get
            {
                if (ViewState["PageIndex"] == null) { return 1; }
                else { return (Int32)ViewState["PageIndex"]; }
            }
        }
        #endregion

        #endregion
        //SQL语句的基础
        分页算法需要的属性#region 分页算法需要的属性
        表名、视图名#region 表名、视图名
        /**//// <summary>
        /// 表名、视图名
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableName
        {
            set { ViewState["TableName"] = value; }
            get
            {
                if (ViewState["TableName"] == null)
                {
                    if ((base.Site != null) && base.Site.DesignMode)
                    {
                        return "TableName";
                    }
                    else
                    {
                        CommandClass.MsgBox("没有设置TableName属性!", true);
                        return null;
                    }
                }
                else { return ViewState["TableName"].ToString(); }
            }
        }
        #endregion

        表的主键#region 表的主键
        /**//// <summary>
        /// 表的主键
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableIDColumns
        {
            set { ViewState["TableIDColumns"] = value; }
            get
            {
                if (ViewState["TableIDColumns"] == null)
                {
                    if ((base.Site != null) && base.Site.DesignMode)
                    {
                        return "TableIDColumns";
                    }
                    else
                    {
                        CommandClass.MsgBox("没有设置TableIDColumns属性!", true);
                        return null;

                    }
                }
                else { return ViewState["TableIDColumns"].ToString(); }
            }
        }
        #endregion

        显示的字段#region 显示的字段
        /**//// <summary>
        /// 显示的字段
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableShowColumns
        {
            set { ViewState["TableShowColumns"] = value; }
            get
            {
                if (ViewState["TableShowColumns"] == null) { return "*"; }
                else { return ViewState["TableShowColumns"].ToString(); }
            }
        }
        #endregion

        排序字段#region 排序字段
        /**//// <summary>
        /// 排序字段
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableOrderColumns
        {
            set { ViewState["TableOrderColumns"] = value; }
            get
            {
                if (ViewState["TableOrderColumns"] == null)
                {
                    if ((base.Site != null) && base.Site.DesignMode)
                    {
                        return "TableOrderColumns";
                    }
                    else
                    {
                        CommandClass.MsgBox("没有设置TableOrderColumns属性!", true);
                        return null;
                    }
                }
                else { return ViewState["TableOrderColumns"].ToString(); }
            }
        }
        
        #endregion

        查询条件#region 查询条件
        /**//// <summary>
        /// 查询条件
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableQuery
        {
            set { ViewState["TableQuery"] = value; }
            get
            {
                if (ViewState["TableQuery"] == null)
                    return "";
                else
                    return ViewState["TableQuery"].ToString();
            }
        }
        
        #endregion
        #endregion

        //SQL模版
        分页算法模版#region 分页算法模版
        /**//// <summary>
        /// 第一页的分页算法模版
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句模版")]
        [DefaultValue("")]
        [Localizable(true)]
        public string SQLGetFirstPage
        {
            set {  ViewState["FirstPageSQL"] = value;  }
            get
            {
                return ViewState["FirstPageSQL"] == null ? null : ViewState["FirstPageSQL"].ToString();
            }
        }
        #endregion

        指定页号的分页算法模版#region 指定页号的分页算法模版
        /**//// <summary>
        /// 指定页号的分页算法模版
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句模版")]
        [DefaultValue("")]
        [Localizable(true)]
        public string SQLGetNextPage
        {
            set
            {
                ViewState["NextPageSQL"] = value;
            }
            get
            {
                return ViewState["NextPageSQL"]==null?null:ViewState["NextPageSQL"].ToString();
            }
        }
        #endregion

        指定页号的分页算法模版#region 指定页号的分页算法模版
        /**//// <summary>
        /// 最后一页的分页算法模版
        /// </summary>
        [Bindable(true)]
        [Category("SQL语句模版")]
        [DefaultValue("")]
        [Localizable(true)]
        public string SQLGetLastPage
        {
            set
            {
                ViewState["LastPageSQL"] = value;
            }
            get
            {
                return ViewState["LastPageSQL"] == null ? null : ViewState["LastPageSQL"].ToString();
            }
        }
        #endregion

        #endregion

        //UI
        页面显示 上一页、下一页#region 页面显示 上一页、下一页

        上一页#region 上一页
        /**//// <summary>
        /// 上一页
        /// </summary>
        [Bindable(true)]
        [Category("页面显示")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIPrev
        {
            get { return pPrevText; }
            set { pPrevText = value; }
        }
        #endregion

        下一页#region 下一页
        /**//// <summary>
        /// 下一页
        /// </summary>
        [Bindable(true)]
        [Category("页面显示")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUINext
        {
            get { return pNextText; }
            set { pNextText = value; }
        }
        #endregion

        首页#region 首页
        /**//// <summary>
        /// 首页
        /// </summary>
        [Bindable(true)]
        [Category("页面显示")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIFirst
        {
            get { return pFirstText; }
            set { pFirstText = value; }
        }
        #endregion

        末页#region 末页
        /**//// <summary>
        /// 末页
        /// </summary>
        [Bindable(true)]
        [Category("页面显示")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUILast
        {
            get { return pLastText; }
            set { pLastText = value; }
        }
        #endregion

        GO#region GO
        /**//// <summary>
        /// GO
        /// </summary>
        [Bindable(true)]
        [Category("页面显示")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIGO
        {
            get { return pGOText; }
            set { pGOText = value; }
        }
        #endregion

        #endregion

        页面导航的数量#region 页面导航的数量
        /**//// <summary>
        /// 
        /// </summary>
        public Int32 NaviCount
        {
            set { ViewState["NaviCount"] = value;  }
            get
            {
                if (ViewState["NaviCount"] == null)
                    return 10;
                else
                    return (Int32)ViewState["NaviCount"];
            }
        }
        #endregion

        页面显示 记录数、页数#region 页面显示 记录数、页数

        共{0}条记录数#region 共{0}条记录数
        /**//// <summary>
        /// 共{0}条记录数
        /// </summary>
        [Bindable(true)]
        [Category("记录信息")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIAllCount
        {
            set { ViewState["PageUIAllCount"] = value; }
            get
            {
                if (ViewState["PageUIAllCount"] == null)
                    return "共<font style=\"color:Red;\">{0}</font>条记录数";
                else
                    return ViewState["PageUIAllCount"].ToString();
            }
        }
        #endregion

        第{0}/{1}页#region 第{0}/{1}页
        /**//// <summary>
        /// 第{0}/{1}页
        /// </summary>
        [Bindable(true)]
        [Category("记录信息")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIAllPage
        {
            set { ViewState["PageUIAllPage"] = value; }
            get
            {
                if (ViewState["PageUIAllPage"] == null)
                    return "第<font style=\"color:Red;\">{0}</font>/<font style=\"color:Red;\">{1}</font>页";
                else
                    return ViewState["PageUIAllPage"].ToString();
            }
        }
        #endregion

        每页{0}条记录#region 每页{0}条记录
        /**//// <summary>
        /// 共{0}条记录数
        /// </summary>
        [Bindable(true)]
        [Category("记录信息")]
        [DefaultValue("")]
        [Localizable(true)]
        public string PageUIAllPageCount
        {
            set { ViewState["PageUIAllPageCount"] = value; }
            get
            {
                if (ViewState["PageUIAllPageCount"] == null)
                    return "每页<font style=\"color:Red;\">{0}</font>条记录";
                else
                    return ViewState["PageUIAllPageCount"].ToString();
            }
        }
        #endregion

        #endregion

        显示数据的控件的ID#region 显示数据的控件的ID
        /**//// <summary>
        /// 共{0}条记录数
        /// </summary>
        [Bindable(true)]
        [Category("显示数据的控件ID")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ControlGridID
        {
            set { ViewState["ControlGridID"] = value; }
            get
            {
                if (ViewState["ControlGridID"] == null)
                    return null;
                else
                    return ViewState["ControlGridID"].ToString();
            }
        }
        #endregion


        数据访问实例的设置#region 数据访问实例的设置
        /**//// <summary>
        /// 设置数据访问层的实例
        /// </summary>
        public DataAccessHelp DAL
        {
            set { dal = value; }
            get
            {
                if (dal == null)
                    dal = new DataAccessHelp();

                return dal;
            }
        }
        #endregion

        管理部分#region 管理部分
        /**//// <summary>
        /// 提取数据的实例
        /// </summary>
        public PageManage.PageGetData ManagerGetData
        {
            set { MgrGetData = value; }
            get
            {
                PageManage.PageGetData tmp = MgrGetData;
                if (tmp == null)
                {
                    tmp = new JYK.Controls.PageManage.PageGetData();
                    MgrGetData = tmp;
                }

                if (tmp.myPage == null)
                    tmp.myPage = this;

                return tmp;
            }
        }

        /**//// <summary>
        /// 提取数据的实例
        /// </summary>
        public PageManage.PageSQL ManagerPageSQL
        {
            set { MgrPageSQL = value; }
            get
            {
                PageManage.PageSQL tmp = MgrPageSQL;
                if (tmp == null)
                {
                    tmp = new JYK.Controls.PageManage.PageSQL();
                    MgrPageSQL = tmp;
                }

                if (tmp.myPage == null)
                    tmp.myPage = this;

                return tmp;
            }
        }

        /**//// <summary>
        /// 提取数据的实例
        /// </summary>
        public PageManage.PageUI ManagerPageUI
        {
            set { MgrPageUI = value; }
            get
            {
                PageManage.PageUI tmp = MgrPageUI;
                if (tmp == null)
                {
                    tmp = new JYK.Controls.PageManage.PageUI();
                    MgrPageUI = tmp;
                }

                if (tmp.myPage == null)
                    tmp.myPage = this;

                return tmp;
            }
        }
        #endregion

        #endregion

        函数#region 函数
        /**//// <summary>
        /// 显示第一页的数据
        /// </summary>
        public void BindFirstPage()
        {
            //生成SQL语句和获取记录总数
            this.ManagerPageSQL.CreateSQL();
            //绑定第一页的数据
            myDataBind(1);
        }
        #endregion

        绘制UI CreateChildControls()#region 绘制UI CreateChildControls()
        /**//// <summary>
        /// 绘制UI
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            LinkButton btn = new LinkButton();
            btn.ID = "Page_Button";
            btn.Text = "";//所有的回发事件
            //btn.Click += new EventHandler(btn_Click);   //添加事件
            this.Controls.Add(btn);

            ManagerPageUI.AddPageUI();  //添加页面显示

            Page_Click();   //调用分页事件
        }
        #endregion

        响应分页事件#region 响应分页事件
        /**//// <summary>
        /// 响应分页事件
        /// </summary>
        private void Page_Click()
        {
            //string a = base.Page.Request.Form["__EVENTTARGET"];
            string PageIndex = base.Page.Request.Form["__EVENTARGUMENT"];

            if (!Functions.IsInt(PageIndex))
                return;
            else
                myDataBind(Int32.Parse(PageIndex));
        }

        /**//// <summary>
        /// 绑定控件
        /// </summary>
        private void myDataBind(Int32 PageIndex)
        {
            if (ControlGrid == null)
            {
                string CID = this.ControlGridID;
                if (CID == null)
                    return;
                else
                    ControlGrid = (WebControl)base.Page.FindControl(CID);
            }

            Int32 NoCount = this.NaviCount; //一组页号的数量

            bool isNavi = false;
            Int32 tmpPageIndex = this.PageIndex;

            switch (PageIndex)
            {
                case 1:   //首页
                    isNavi = true;
                    break;

                case -3:
                    //上一页
                    PageIndex = this.PageIndex - 1;
                    break;

                case -2:
                    //下一页
                    PageIndex = this.PageIndex + 1;
                    if (PageIndex % NoCount == 1)
                        isNavi = true;

                    break;

                case -99:   //前导
                    if (tmpPageIndex % NoCount == 0)
                        PageIndex = tmpPageIndex - NoCount;
                    else
                        PageIndex = tmpPageIndex / NoCount * NoCount;

                    isNavi = true;
                    break;

                case -88:   //后导
                    if (tmpPageIndex % NoCount == 0)
                        PageIndex = tmpPageIndex + NoCount;
                    else 
                        PageIndex = (this.PageIndex / NoCount + 1) * NoCount + 1;

                    isNavi = true;
                    break;

                

                default:
                    isNavi = false ;
                    break;
            }

            if (PageIndex == this.PageCount)
                isNavi = true;  //末页
                    
            this.PageIndex = PageIndex;

            //获取记录集
            DataTable dt = this.ManagerGetData.GetDataTable(PageIndex);
            //通过(好像是反射)来设置 DataSource属性。
            Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(ControlGrid, null, "datasource", new object[] { dt }, null, null);
            ControlGrid.DataBind();

            //重新显示当前页号
            this.ManagerPageUI.UpdatePageIndex();

            //重新显示页号当航
            if (isNavi)
                this.ManagerPageUI.ReloadPageNavi();
            else         
                this.ManagerPageUI.UpdatePageNavi();
            //base.Page.Response.Write(a + "+" + PageIndex);
        }
        #endregion

        /**//// <summary>
        /// 接收分页控件的回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            //base.Page.Response.Write("ss");

        }

        /**//// <summary>
        /// 分页的回发事件,没有成功
        /// </summary>
        /// <param name="PageIndex"></param>
        public void RaisePostBackEvent(string PageIndex)
        {
            //base.Page.Response.Write("www");
            //Page_Click();
        }


        设计时支持#region 设计时支持
        /**//// <summary>
        /// 设计时支持
        /// </summary>
        /// <param name="output"></param>
        protected override void Render(HtmlTextWriter output)
        {
            if ((base.Site != null) && base.Site.DesignMode)
            {
                output.Write("<div style='TEXT-ALIGN: center;width:100%'>第1/100页 &nbsp;&nbsp; 首页&nbsp;&nbsp;上一页&nbsp;&nbsp;[1][2][3] &nbsp;&nbsp; 下一页 &nbsp;&nbsp; 末页 &nbsp;&nbsp; 共1000条记录</div>");
            }
            else
            {
                //Page_Click();
                output.Write("<div id='" + this.ClientID + "Page' style='TEXT-ALIGN: center;width:90%'>");
                base.Render(output);
                output.Write("</div>");
            }

        }
        #endregion

    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Golang语言社区

文件上传下载

package main import ( "fmt" "html/template" "log" "net/http" ...

45018
来自专栏互联网杂技

Angularjs中UI Router超级详细的教程{{下}}

接着上一 state间如何传字符串参数 在路由中这样设置: .state('content.photos.detail.comment',{ url:'/co...

4865
来自专栏Java成神之路

Java微信公众平台开发_04_自定义菜单

自定义菜单中请求包的数据是Json字符串格式的,请参见:  Java_数据交换_fastJSON_01_用法入门

1433
来自专栏JadePeng的技术博客

使用websocket-sharp来创建c#版本的websocket服务

当前有一个需求,需要网页端调用扫描仪,javascript不具备调用能力,因此需要在机器上提供一个ws服务给前端网页调用扫描仪。而扫描仪有一个c#版本的API,...

7585
来自专栏Alan's Lab

[From Nand to Tetris] 第8章 虚拟机项目 python 实现

为防闲逛至此的看官不知所云: From Nand to Tetris 是一个在线课程,目标是指导学生从 Nand 逻辑门开始从头到尾完成一整套计算机系统。

732
来自专栏xingoo, 一个梦想做发明家的程序员

MFC 随机矩形

问题描述:   简单地使用随即的尺寸和颜色不停的绘制一系列的图像。 一种古老的方式:   设置一个向窗口函数发送WM_TIMER消息的windows计时器。  ...

2095
来自专栏Golang语言社区

Go中的依赖注入

I have written a small utility package to handle dependency injection in Go (it'...

2554
来自专栏ios 技术积累

MJRefresh 封装

MJRefresh是很好用的一个刷新控件,但是如果在每个ViewController中都加入如下代码

2542
来自专栏菩提树下的杨过

Silverlight:利用异步加载Xap实现自定义loading效果

关键点: 1.利用WebClient的DownloadProgressChanged事件更新下载进度 2.下载完成后,分析Xap包的程序集Assembly信息 ...

21310
来自专栏葡萄城控件技术团队

挂载非引用Assembly中的事件

有两个 Assembly A,B 在A中声明了一个事件(某个类中的某个事件), B没有引用这Assembly ,但是想挂A中的这个事件。当A中的某个逻辑fire...

1955

扫码关注云+社区

领取腾讯云代金券