翻页控件示例代码

using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Pagination
{
    /// <summary>
    /// 分页控件
    /// </summary>
    public class PaginationControl : System.Web.UI.WebControls.WebControl
    {
        #region Fields
        private Button btnPreviousPage;
        private Button btnNextPage;
        private Label lblGo;
        private TextBox txtCurrentPage;
        private Button btnGo;
        private Label lblPageMessage;

        //事件
        public event System.EventHandler PreviousPageClick;
        public event System.EventHandler NextPageClick;
        public event System.EventHandler GoPageClick;

        private int _currentPage;
        #endregion

        #region Properties
        /// <summary>
        /// 控件Enabled属性
        /// </summary>
        public bool ControlEnabled
        {
            set
            {
                this.btnPreviousPage.Enabled = value;
                this.btnNextPage.Enabled = value;
                this.txtCurrentPage.Enabled = value;
                this.btnGo.Enabled = value;
            }
        }


        /// <summary>
        /// 当前页,只读
        /// </summary>
        public int CurrentPage
        {
            get
            {
                string currentPage = lblPageMessage.Text.Trim();
                return int.Parse(currentPage.Substring(currentPage.IndexOf("第") + 1, currentPage.IndexOf("页") - currentPage.IndexOf("第") - 1));
            }
        }


        /// <summary>
        /// 总页数,只读
        /// </summary>
        public int TotalPage
        {
            get
            {
                string totalPage = lblPageMessage.Text.Trim();
                return int.Parse(totalPage.Substring(totalPage.IndexOf("共") + 1, totalPage.LastIndexOf("页") - totalPage.IndexOf("共") - 1));
            }
        }
        #endregion

        #region Methods
        #region Override Methods
        protected override void OnInit(EventArgs e)
        {
            if (ChildControlsCreated)
                return;
            else
            {
                this.Height = 20;

                if (btnPreviousPage == null)
                {
                    btnPreviousPage = new Button();
                    btnPreviousPage.Text = "上页";
                    btnPreviousPage.BackColor = System.Drawing.Color.White;
                    btnPreviousPage.ForeColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    btnPreviousPage.BorderStyle = BorderStyle.None;
                    btnPreviousPage.Height = 14;
                    Controls.Add(btnPreviousPage);
                }

                if (btnNextPage == null)
                {
                    btnNextPage = new Button();
                    btnNextPage.Text = "下页";
                    btnNextPage.BackColor = System.Drawing.Color.White;
                    btnNextPage.ForeColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    btnNextPage.BorderStyle = BorderStyle.None;
                    btnNextPage.Height = 14;
                    Controls.Add(btnNextPage);
                }

                if (lblGo == null)
                {
                    lblGo = new Label();
                    lblGo.Text = "转到:";
                    lblGo.BackColor = System.Drawing.Color.White;
                    lblGo.ForeColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    lblGo.CssClass = "wenzi";
                    Controls.Add(lblGo);
                }

                if (txtCurrentPage == null)
                {
                    txtCurrentPage = new TextBox();
                    //     txtCurrentPage.Width = 35;
                    txtCurrentPage.BackColor = System.Drawing.Color.White;
                    txtCurrentPage.BorderColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    txtCurrentPage.BorderWidth = 1;
                    txtCurrentPage.Width = 40;
                    txtCurrentPage.Height = 16;
                    txtCurrentPage.BorderStyle = BorderStyle.Solid;
                    txtCurrentPage.CssClass = "wenzi";
                    Controls.Add(txtCurrentPage);
                }

                if (btnGo == null)
                {
                    btnGo = new Button();
                    btnGo.Text = "GO";
                    btnGo.BackColor = System.Drawing.Color.White;
                    btnGo.BorderColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    btnGo.ForeColor = System.Drawing.Color.FromArgb(0, 51, 102);
                    btnGo.BorderWidth = 1;
                    btnGo.Height = 16;
                    btnGo.Font.Size = FontUnit.Point(9);
                    Controls.Add(btnGo);
                }

                if (lblPageMessage == null)
                {
                    lblPageMessage = new Label();
                    lblPageMessage.Text = "第0页/共0页";
                    lblPageMessage.BackColor = System.Drawing.Color.White;
                    lblPageMessage.ForeColor = System.Drawing.Color.Black;
                    lblPageMessage.CssClass = "wenzi";
                    Controls.Add(lblPageMessage);
                }

                this.btnPreviousPage.Click += new System.EventHandler(PreviousPage_Click);
                this.btnNextPage.Click += new System.EventHandler(NextPage_Click);
                this.btnGo.Click += new System.EventHandler(GoPage_Click);

                ChildControlsCreated = true;
            }
        }


        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;
            }
        }


        /// <summary>
        /// 将此控件呈现给指定的输出参数。
        /// </summary>
        /// <param name="output"> 要写出到的 HTML 编写器 </param>
        protected override void Render(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Table);
            output.RenderBeginTag(HtmlTextWriterTag.Tr);

            output.RenderBeginTag(HtmlTextWriterTag.Td);
            btnPreviousPage.RenderControl(output);
            btnNextPage.RenderControl(output);
            lblGo.RenderControl(output);
            txtCurrentPage.RenderControl(output);
            btnGo.RenderControl(output);
            lblPageMessage.RenderControl(output);
            output.RenderEndTag();//td的结束

            output.RenderEndTag();//tr的结束
            output.RenderEndTag();
        }
        #endregion

        #region
        /// <summary>
        /// 设置当前页数,并显示。
        /// </summary>
        /// <param name="currentPage">当前页码</param>
        /// <returns></returns>
        private void SetCurrentPage(int currentPage)
        {
            lblPageMessage.Text = lblPageMessage.Text.Remove(1, lblPageMessage.Text.IndexOf("页") - 1).Insert(1, currentPage.ToString());
            txtCurrentPage.Text = currentPage.ToString();
        }


        /// <summary>
        /// 计算总页数,并显示
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalRow">总记录数</param>
        /// <returns>总页数</returns>
        private int CalTotalPage(int pageSize, int totalRow)
        {
            int totalPage;

            if (pageSize <= 0)
                totalPage = 0;
            else if (pageSize == 1)
                totalPage = totalRow;
            else
            {
                string page = Math.Floor(totalRow / pageSize).ToString();
                if (totalRow % pageSize == 0)
                {
                    totalPage = int.Parse(page);
                }
                else
                {
                    totalPage = int.Parse(page) + 1;
                }
            }

            lblPageMessage.Text = lblPageMessage.Text.Remove(lblPageMessage.Text.IndexOf("共") + 1, lblPageMessage.Text.LastIndexOf("页") - lblPageMessage.Text.IndexOf("共") - 1);
            lblPageMessage.Text = lblPageMessage.Text.Insert(lblPageMessage.Text.IndexOf("共") + 1, totalPage.ToString());

            return totalPage;
        }


        /// <summary>
        /// 计算起始行位置
        /// </summary>
        /// <param name="currentPage">当前页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns>起始行位置</returns>
        private int CalStartRow(int pageSize)
        {
            if (this.CurrentPage <= 1 || pageSize <= 0)
                return 1;
            else
                return (this.CurrentPage - 1) * pageSize;
        }


        /// <summary>
        /// 计算起始行位置
        /// </summary>
        /// <param name="currentPage">当前页码</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns>起始行位置</returns>
        private int CalStartRow(int currentPage, int pageSize)
        {
            if (currentPage <= 1 || pageSize <= 0)
                return 1;
            else
                return (currentPage - 1) * pageSize;
        }


        /// <summary>
        /// 格式化查询语句,以便查询分页(计算总行数)。
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public string FormatSql(string sql)
        {
            //查询语句中存在union,或者没有fRoM标记。
            if (sql.ToLower().IndexOf("union") >= 0 || sql.IndexOf("fRoM") < 0)
            {
                sql = "select count(*) from (" + sql + ")";
                return sql;
            }
            else
            {
                return FormatSql(sql, "fRoM");
            }
        }


        /// <summary>
        /// 格式化查询语句,以便查询分页(计算总行数)。
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="searchString">查找字符串</param>
        /// <returns></returns>
        public string FormatSql(string sql, string searchString)
        {
            sql = sql.Remove(0, sql.IndexOf(searchString) + searchString.Length);
            sql = "select count(*) from" + sql;
            return sql;
        }


        /// <summary>
        /// 格式化查询语句,以便查询分页。
        /// 查询语句一定要有where关键字。
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRow">总记录数</param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        public string FormatSql(string sql, int pageSize, int totalRow, int currentPage)
        {
            int totalPage = CalTotalPage(pageSize, totalRow);

            if (totalPage <= 1)
                this.ControlEnabled = false;
            else
                this.ControlEnabled = true;

            //设置当前页码
            SetCurrentPage(currentPage);

            //记录标志位置
            int markPosition = 0;

            //如果查询语句中使用union
            if (sql.ToLower().IndexOf("union") >= 0 || sql.IndexOf("fRoM") < 0)
            {
                if (this.CurrentPage <= 1 || totalPage <= 1)
                {
                    sql = "select * from (" + sql + ") where rownum <= " + pageSize.ToString();
                    return sql;
                }
                else
                {
                    int startRow, endRow;

                    startRow = CalStartRow(pageSize);
                    endRow = startRow + pageSize;

                    sql = "select * from (select rownum rn,a.* from (" + sql + ") a where rownum <= " + endRow.ToString() + ") where rn > " + startRow.ToString();
                    return sql;
                }
            }
            else
            {
                if (this.CurrentPage <= 1 || totalPage <= 1)
                {
                    markPosition = sql.IndexOf("wHerE");
                    sql = sql.Insert(markPosition + 5, " rownum <= " + pageSize.ToString() + " and");
                    return sql;
                }
                else
                {
                    int startRow, endRow;

                    startRow = CalStartRow(pageSize);
                    endRow = startRow + pageSize;

                    markPosition = sql.IndexOf("fRoM");
                    sql = "select * from (" + sql.Insert(markPosition, ",rownum rn ");

                    markPosition = sql.IndexOf("wHerE");
                    sql = sql.Insert(markPosition + 5, " rownum <= " + endRow.ToString() + " and") + ") where rn > " + startRow.ToString();

                    return sql;
                }
            }
        }


        /// <summary>
        /// 格式化查询语句,以便查询分页。
        /// 查询语句一定要有where关键字,当前页默认为1。
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRow">总记录数</param>
        /// <returns></returns>
        public string FormatSql(string sql, int pageSize, int totalRow)
        {
            return FormatSql(sql, pageSize, totalRow, 1);
        }

        #endregion
        #endregion

        #region Events
        private void PreviousPage_Click(object sender, System.EventArgs e)
        {
            _currentPage = this.CurrentPage;
            if (_currentPage > 1)
            {
                _currentPage -= 1;
                lblPageMessage.Text = lblPageMessage.Text.Remove(1, lblPageMessage.Text.IndexOf("页") - 1).Insert(1, _currentPage.ToString());
                txtCurrentPage.Text = _currentPage.ToString();
            }

            if (PreviousPageClick != null)
            {
                PreviousPageClick(this, e);
            }
        }


        private void NextPage_Click(object sender, System.EventArgs e)
        {
            _currentPage = this.CurrentPage;
            if (_currentPage < this.TotalPage)
            {
                _currentPage += 1;
                lblPageMessage.Text = lblPageMessage.Text.Remove(1, lblPageMessage.Text.IndexOf("页") - 1).Insert(1, _currentPage.ToString());
                txtCurrentPage.Text = _currentPage.ToString();
            }

            if (NextPageClick != null)
            {
                NextPageClick(this, e);
            }
        }


        private void GoPage_Click(object sender, System.EventArgs e)
        {
            try
            {
                _currentPage = int.Parse(txtCurrentPage.Text.Trim());
                if (_currentPage <= 0 || _currentPage > this.TotalPage)
                    return;
                else
                {
                    lblPageMessage.Text = lblPageMessage.Text.Remove(1, lblPageMessage.Text.IndexOf("页") - 1).Insert(1, _currentPage.ToString());
                    txtCurrentPage.Text = _currentPage.ToString();
                }
            }
            catch
            {
                return;
            }
            if (GoPageClick != null)
            {
                GoPageClick(this, e);
            }
        }
        #endregion
    }
}

 

 

posted on 2010-07-01 23:59  洞幺人生  阅读(258)  评论(0编辑  收藏  举报