Click here to Skip to main content
15,895,142 members
Articles / Web Development / ASP.NET

Custom Paging with User Control

Rate me:
Please Sign up or sign in to vote.
4.32/5 (18 votes)
16 Mar 20045 min read 160.1K   2.7K   64  
Promote Code Reuse by Using a User Control to Do Custom Paging
namespace WebApplication1
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
    using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

    //public deletgates
    public delegate void FirstPageEventHandler(object sender, DataNavigatorEventArgs e);
    public delegate void LastPageEventHandler(object sender, DataNavigatorEventArgs e);
    public delegate void PreviousPageEventHandler(object sender, DataNavigatorEventArgs e);
    public delegate void NextPageEventHandler(object sender, DataNavigatorEventArgs e);
    public delegate void PageChangedEventHandler(object sender, DataNavigatorEventArgs e);

	/// <summary>
	///		Summary description for DataNavigator.
	/// </summary>
    public class DataNavigator : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.Label lblCurrentPage;
        protected System.Web.UI.WebControls.Label lblTotalPages;
        protected System.Web.UI.WebControls.ImageButton btnPrevious;
        protected System.Web.UI.WebControls.ImageButton btnFirst;
        protected System.Web.UI.WebControls.ImageButton btnLast;
        protected System.Web.UI.WebControls.DropDownList ddPage;
        protected System.Web.UI.WebControls.ImageButton btnNext;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Put user code to initialize the page here
                int iPages = this.TotalPages;

                ddPage.Items.Clear();

                if (iPages > 0)
                {
                    for (int i = 1; i <= iPages; i++)
                        ddPage.Items.Add(i.ToString());
                }
            }
        }

        //public events
        public event FirstPageEventHandler FirstPage;
        public event LastPageEventHandler LastPage;
        public event PreviousPageEventHandler PreviousPage;
        public event NextPageEventHandler NextPage;
        public event PageChangedEventHandler PageChanged;

        protected void OnPageChangedButton(object sender, EventArgs e)
        {
            DataNavigatorEventArgs args = new DataNavigatorEventArgs();
            args.CurrentPage = int.Parse(ddPage.SelectedItem.Text);
            args.TotalPages = int.Parse(lblTotalPages.Text);
            OnPageChanged(args);
        }

        protected virtual void OnPageChanged(DataNavigatorEventArgs args)
        {
            if (PageChanged != null)
            {
                // Invoke the delegates.
                PageChanged(this, args);
            }
        }

        protected void OnPreviousPageButton(object sender, ImageClickEventArgs e)
        {
            DataNavigatorEventArgs args = new DataNavigatorEventArgs();
            args.CurrentPage = int.Parse(lblCurrentPage.Text);
            args.TotalPages = int.Parse(lblTotalPages.Text);

            SetDropDownPageNumber(args.CurrentPage - 1);

            OnPreviousPage(args);
        }

        protected virtual void OnPreviousPage(DataNavigatorEventArgs args)
        {
            if (PreviousPage != null)
            {
                // Invoke the delegates.
                PreviousPage(this, args);
            }
        }

        protected void OnNextPageButton(object sender, ImageClickEventArgs e)
        {
            DataNavigatorEventArgs args = new DataNavigatorEventArgs();
            args.CurrentPage = int.Parse(lblCurrentPage.Text);
            args.TotalPages = int.Parse(lblTotalPages.Text);

            SetDropDownPageNumber(args.CurrentPage + 1);

            OnNextPage(args);
        }

        protected virtual void OnNextPage(DataNavigatorEventArgs args)
        {
            if (NextPage != null)
            {
                // Invoke the delegates.
                NextPage(this, args);
            }
        }

        protected void OnFirstPageButton(object sender, ImageClickEventArgs e)
        {
            DataNavigatorEventArgs args = new DataNavigatorEventArgs();
            args.CurrentPage = int.Parse(lblCurrentPage.Text);
            args.TotalPages = int.Parse(lblTotalPages.Text);

            SetDropDownPageNumber(1);

            OnFirstPage(args);
        }

        protected virtual void OnFirstPage(DataNavigatorEventArgs args)
        {
            if (FirstPage != null)
            {
                // Invoke the delegates.
                FirstPage(this, args);
            }
        }

        protected void OnLastPageButton(object sender, ImageClickEventArgs e)
        {
            DataNavigatorEventArgs args = new DataNavigatorEventArgs();
            args.CurrentPage = int.Parse(lblCurrentPage.Text);
            args.TotalPages = int.Parse(lblTotalPages.Text);

            SetDropDownPageNumber(args.TotalPages);

            OnLastPage(args);
        }

        protected virtual void OnLastPage(DataNavigatorEventArgs args)
        {
            if (LastPage != null)
            {
                // Invoke the delegates.
                LastPage(this, args);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
		
        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnPrevious.Click += new System.Web.UI.ImageClickEventHandler(this.OnPreviousPageButton);
            this.btnNext.Click += new System.Web.UI.ImageClickEventHandler(this.OnNextPageButton);
            this.btnFirst.Click += new System.Web.UI.ImageClickEventHandler(this.OnFirstPageButton);
            this.btnLast.Click += new System.Web.UI.ImageClickEventHandler(this.OnLastPageButton);
            this.ddPage.SelectedIndexChanged += new EventHandler(this.OnPageChangedButton);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Get/Set Properties
        public int CurrentPage
        {
            get { return int.Parse(lblCurrentPage.Text); }
            set { lblCurrentPage.Text = Convert.ToString(value); }
        }

        public int TotalPages
        {
            get { return int.Parse(lblTotalPages.Text); }
            set { lblTotalPages.Text = Convert.ToString(value); }
        }

        public bool NextButtonEnabled
        {
            get { return btnNext.Enabled; }
            set { btnNext.Enabled = value; }
        }

        public string NextButtonImageUrl
        {
            get { return btnNext.ImageUrl; }
            set { btnNext.ImageUrl = value; }
        }

        public bool PreviousButtonEnabled
        {
            get { return btnPrevious.Enabled; }
            set { btnPrevious.Enabled = value; }
        }

        public string PreviousButtonImageUrl
        {
            get { return btnPrevious.ImageUrl; }
            set { btnPrevious.ImageUrl = value; }
        }

        public bool FirstButtonEnabled
        {
            get { return btnFirst.Enabled; }
            set { btnFirst.Enabled = value; }
        }

        public string FirstButtonImageUrl
        {
            get { return btnFirst.ImageUrl; }
            set { btnFirst.ImageUrl = value; }
        }

        public bool LastButtonEnabled
        {
            get { return btnLast.Enabled; }
            set { btnLast.Enabled = value; }
        }

        public string LastButtonImageUrl
        {
            get { return btnLast.ImageUrl; }
            set { btnLast.ImageUrl = value; }
        }
        #endregion

        private void SetDropDownPageNumber(int iCurrentPage)
        {
            if (ddPage.Items.Count > 0)
                // since SelectedIndex is 0-based, we have to
                // take the current page number and minus 1
                ddPage.SelectedIndex = iCurrentPage - 1;
        }
    }

    public class DataNavigatorEventArgs : EventArgs
    {
        private int m_iCurrentPage;
        private int m_iTotalPages;

        public DataNavigatorEventArgs()
        {
        }

        public int CurrentPage
        {
            get { return m_iCurrentPage; }
            set { m_iCurrentPage = value; }
        }

        public int TotalPages
        {
            get { return m_iTotalPages; }
            set { m_iTotalPages = value; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Linus has more than 10 years of experience in designing and implementing enterprise scale applications. He is a seasoned architect in both J2EE and Microsoft technologies. He is also a Microsoft Certified Solution Developer for .NET.

Comments and Discussions