Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Advanced Paging GridView with ASP.NET 2.0/3.5

, 7 Nov 2008
Advanced paging GridView with ASP.NET 2.0/3.5
WizardGridView.zip
WizardGridView
Demo
App_Data
Bin
WizardGridView.dll
WizardGridView.pdb
WizardGridView
bin
Debug
WizardGridView.dll
Properties
Resources
WizardGridView.bmp
WizardGridView.suo
using System;
using System.Globalization;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using Microsoft.VisualBasic;
using System.Drawing;

[assembly: WebResource("WizardGridView.bmp", "image/bmp")]
[assembly: WebResource("WizardGridView.Resources.WizardGridView.css", "text/css")]
namespace WizardGridView
{
    /// <summary>
    /// Advanced pager function GridView
    /// </summary>
    /// <remarks>
    /// <list type="bullet">
    /// <item><description>�}�o�H���i�z�L VS 2005 / 2008�A�ۭq�u��ƦC�B���ƦC�B�ƹ��m���W����ƦC�v���I���C��C</description></item>
    /// <item><description>�ϥΪ̥i�ۦ��J�����Ʀr�A�����e���S�w�����C</description></item>
    /// <item><description>�ثe������覡�@�߬��u�a�k�v�A�������覡�@�߬��u�a�W�v�A�r��j�p�T�w���p���r�A���}�o���}�o�H���ק�C</description></item>
    /// </list>
    /// </remarks>
    [ToolboxData("<{0}:WizardGridView runat=server></{0}:WizardGridView>"),
     ToolboxBitmap(typeof(WizardGridView), "WizardGridView.bmp")]
    public class WizardGridView : System.Web.UI.WebControls.GridView
    {
        //private WizardGridView.PagingStyleCollection _pagingStyle;
        private int? _recordCount = null;       // ObjectDataSource �̼��o���`����
        //private int _recordCount = null;       // ObjectDataSource �̼��o���`����

        // �������覡
        //private PagerPosition _psVerticalAlignment = PagerPosition.Top;

        // ����
        private System.Drawing.Color FOddRowBackColor = System.Drawing.Color.Empty;         // ��ƦC���I���C��
        private System.Drawing.Color FEvenRowBackColor = System.Drawing.Color.Empty;        // ���ƦC���I���C��
        private System.Drawing.Color FMouseOverBackColor = System.Drawing.Color.Empty;      // �ƹ����ܸ�ƦC�W�ɪ��I���C��

        // �r��j�p
        private FontUnit _fontSize = FontUnit.Empty;


        // ���� - ��ƦC���I���C��
        /// <summary>
        /// ��ƦC���I���C��C
        /// </summary>
        [System.ComponentModel.DefaultValue(""),
        System.ComponentModel.Category("Wizard - Appearance"),
        System.ComponentModel.Description("The background color of odd row.")]
        public System.Drawing.Color OddRowBackColor
        {
            get
            {
                return FOddRowBackColor;
            }
            set
            {
                FOddRowBackColor = value;
            }
        }


        // ���� - ���ƦC���I���C��        
        /// <summary>
        /// ���ƦC���I���C��C
        /// </summary>
        [System.ComponentModel.DefaultValue(""),
        System.ComponentModel.Category("Wizard - Appearance"),
        System.ComponentModel.Description("The background color of even row.�C")]
        public System.Drawing.Color EvenRowBackColor
        {
            get
            {
                return FEvenRowBackColor;
            }
            set//(value As System.Drawing.Color)
            {
                FEvenRowBackColor = value;
            }
        }


        // ���� - �ϥΪ̷ƹ��m���W�ɪ���ƦC���I���C��              
        /// <summary>
        /// �ϥΪ̷ƹ��m���W�ɪ���ƦC���I���C��C
        /// </summary>
        [System.ComponentModel.DefaultValue(""),
        System.ComponentModel.Category("Wizard - Appearance"),
        System.ComponentModel.Description("The background color of row when mouse over.")]
        public System.Drawing.Color MouseOverBackColor
        {
            get
            {
                return FMouseOverBackColor;
            }
            set//(value As System.Drawing.Color)
            {
                FMouseOverBackColor = value;
            }
        }


        /// <summary>
        /// ��� WizardGridView ����A�]�A�u���X�C�v�̪��r��j�p (�w�]�� Small)�C
        /// </summary>
        [System.ComponentModel.DefaultValue(""),
        System.ComponentModel.Category("Wizard - Appearance"),
        System.ComponentModel.Description("The font size in whole WizardGridVeiw (default is Small). It would not be affected by HTML.")]
        public FontUnit CustomFontSize  // System.Drawing.Color EvenRowBackColor
        {
            get
            {
                return _fontSize;
            }
            set
            {
                _fontSize = value;
            }
        }


        // // �������覡
        // [System.ComponentModel.DefaultValue("PagerPosition.Top"),
        // System.ComponentModel.Category("Appearance"),
        //System.ComponentModel.Description("�������覡")]
        // public PagerPosition VerticalAlignment
        // {
        //     get
        //     {
        //         return _psVerticalAlignment;
        //     }
        //     set
        //     {
        //         _psVerticalAlignment = value;
        //     }
        // }


        // ����
        protected override void OnRowDataBound(System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            // ��ƦC�B���ƦC�B�ƹ����ܸ�ƦC�W�ɡA���w�]�I���C��C
            // �Y�}�o�H���� GridView ���u�ݩʡv�����h�]�w�ۭq�C��A�h�H�}�o�H�����]�w���ǡA�_�h�H�U�C���w�]�Ȭ���
            if (OddRowBackColor == System.Drawing.Color.Empty)
                OddRowBackColor = System.Drawing.Color.FromName("#FFFFFF");        // �զ�
            if (EvenRowBackColor == System.Drawing.Color.Empty)
                EvenRowBackColor = System.Drawing.Color.FromName("#F7F6F3");       // �L�Ŧ�            
            if (MouseOverBackColor == System.Drawing.Color.Empty)
                MouseOverBackColor = System.Drawing.Color.FromName("#FFFF77");     // �̶���

           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.ForeColor = System.Drawing.Color.Orange;

                if (e.Row.RowIndex % 2 == 0)    // ��ƦC
                {
                    if (FOddRowBackColor != System.Drawing.Color.Empty)
                    {
                        e.Row.Style[HtmlTextWriterStyle.BackgroundColor] = ColorTranslator.ToHtml(FOddRowBackColor);
                    }

                    if (FMouseOverBackColor != System.Drawing.Color.Empty)
                    {
                        // �ƹ����ܸ�ƦC�W���C��
                        e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(FMouseOverBackColor) + "'";
                        //e.Row.Attributes["onmouseover"] = "this.style.color='" + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Orange) + "'";
                        // �ƹ����}��ƦC�W���C��
                        e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(FOddRowBackColor) + "'";
                        //e.Row.Attributes["onmouseover"] = "this.style.color='" + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Black) + "'";
                    }
                }
                else    // ���ƦC
                {
                    if (FEvenRowBackColor != System.Drawing.Color.Empty)
                    {
                        e.Row.Style[HtmlTextWriterStyle.BackgroundColor] = ColorTranslator.ToHtml(FEvenRowBackColor);
                    }

                    if (FMouseOverBackColor != System.Drawing.Color.Empty)
                    {
                        // �ƹ����ܸ�ƦC�W���C��
                        e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(FMouseOverBackColor) + "'";
                        //e.Row.Attributes["onmouseover"] = "this.style.color='" + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Orange) + "'";
                        // �ƹ����}��ƦC�W���C��
                        e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='" + System.Drawing.ColorTranslator.ToHtml(FEvenRowBackColor) + "'";
                        //e.Row.Attributes["onmouseover"] = "this.style.color='" + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Black) + "'";
                    }
                }
            }
            else if (e.Row.RowType == DataControlRowType.Pager)
            {
                e.Row.BackColor = System.Drawing.Color.White;
                e.Row.HorizontalAlign = HorizontalAlign.Right;

            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.BackColor = System.Drawing.Color.FromName("#E7E7E7");     // ���D�I����: �L�Ŧ�
                e.Row.Font.Bold = true;
                e.Row.HorizontalAlign = HorizontalAlign.Center;
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.BackColor = System.Drawing.Color.FromName("#5D7B9D");     // ���ݭI����: �t�Ŧ�
                e.Row.Font.Bold = true;
            }

            base.OnRowDataBound(e);

        } // end of OnRowDataBound()


        protected override void OnPreRender(EventArgs e)
        {
            // 2008/09/17 �s�W�A�]�w��� WizardGridView ���󪺦r��j�p            
            if (this.CustomFontSize == FontUnit.Empty)
            {
                this.CustomFontSize = FontUnit.Small;
                this.Font.Size = CustomFontSize;
            }
            else
            {
                this.Font.Size = CustomFontSize;
            }


            // Add style sheet to parent page 
            string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(),
            "WizardGridView.Resources.WizardGridView.css");

            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssUrl;
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");

            this.Page.Header.Controls.Add(cssLink);

            // Add class name 
            this.CssClass = "pagination";
            this.CssClass = "current";
            this.CssClass = "disabled";

            base.OnPreRender(e);
        }


        #region **** not use ***
        //// ����
        //protected override void OnDataBound(System.Web.UI.WebControls.GridViewRowEventArgs e)
        //{

        //}

        //protected override void OnRowCreated(GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.Pager)
        //    {
        //        //LinkButton First = new LinkButton();
        //        //LinkButton Prev = new LinkButton();
        //        //LinkButton Next = new LinkButton();
        //        //LinkButton Last = new LinkButton();
        //        HtmlInputText InputPage = new HtmlInputText();
        //        Button GoPage = new Button();

        //        TableCell tc = new TableCell();

        //        //e.Row.Controls.Clear();

        //        tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        //        if (_recordCount.HasValue)
        //        {
        //            tc.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�`�@���ơG</span><span style='color:red;font-size:9pt'>" + _recordCount.ToString() + "</span>"));
        //            tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        //            tc.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�C�����ơG</span><span style='color:red;font-size:9pt'>" + PageSize.ToString() + "</span>"));
        //            tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
        //        }
        //        tc.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�ثe�G</span><span style='color:red;font-size:9pt'>" + (PageIndex + 1).ToString()));
        //        tc.Controls.Add(new LiteralControl("/"));
        //        tc.Controls.Add(new LiteralControl(PageCount.ToString() + "</span>"));
        //        tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));

        //        InputPage.ID = "txtPageIndex";
        //        InputPage.Style.Add(HtmlTextWriterStyle.Width, "20px");
        //        tc.Controls.Add(InputPage);

        //        GoPage.Text = "GO";
        //        GoPage.ID = "GO";
        //        GoPage.Click += new System.EventHandler(OnGoPage_Click);
        //        tc.Controls.Add(GoPage);

        //        tc.ColumnSpan = this.Columns.Count;
        //        e.Row.Controls.Add(tc);

        //        InputPage.Attributes.Add("onkeypress", "if(event.keyCode==13){ this.nextSibling.click();event.returnValue=false;}");
        //    }

        //      base.OnRowCreated(e);            
        //}
        #endregion **** not use ***


        /// <summary>
        /// ��ϥΪ̫��U�uGO�v���s�ɡA�ҭn���檺��ơC
        /// </summary>
        private void OnGoPage_Click(object sender, System.EventArgs e)
        {
            HtmlInputText tb = null;
            //tb = (TextBox)this.Page.FindControl("txtPageIndex");

            tb = (HtmlInputText)this.TopPagerRow.FindControl("txtPageIndex");           // ���ݭ��X�C���A��J�u�����v������
            //tb2 = (HtmlInputText)this.BottomPagerRow.FindControl("txtPageIndex");     // �������X�C���A��J�u�����v������

            int pageIndex = -1;     // ���e��������

            if (int.TryParse(tb.Value.Trim(), out pageIndex))
            {
                pageIndex = pageIndex - 1;
                if (pageIndex > -1)
                {
                    //                    this.PageIndex = pageIndex;

                    //int jjjj1 = this.SelectArguments.StartRowIndex;
                    //int jjjj2 = this.PageSize;
                    //string jjjj3 = _recordCount.ToString();
                    //int jjjj4 = this.PageCount;

                    // �Y�ϥΪ̿�J�����ơA�j��J�����`���ơA�h�N GridView ���̤ܳj����
                    if (pageIndex > this.PageCount - 1)
                    {
                        this.PageIndex = this.PageCount - 1;
                    }
                    else  // �Y�ϥΪ̿�J�X�z������(�p��ε���J�����`����)
                    {
                        this.PageIndex = pageIndex;
                    }
                }
            }

            tb.Value = string.Empty;    // �M�ŨϥΪ̿�J������
            //tb2.Value = string.Empty;    // �M�ŨϥΪ̿�J������
        }


        ///// <summary>
        ///// �Τ_�˯���ƪ� DataSourceSelectArguments ����C�w�]�� Empty ��
        ///// </summary>
        //protected DataSourceSelectArguments SelectArguments
        //{
        //    get
        //    {
        //        if (selectArguments == null)
        //        {
        //            selectArguments = CreateDataSourceSelectArguments();
        //        }
        //        return selectArguments;
        //    }
        //}


        protected override void OnLoad(EventArgs e)
        {
            // ���� ObjectDataSource
            ObjectDataSource ods = this.Page.FindControl(this.DataSourceID) as ObjectDataSource;    //������
            //ObjectDataSource ods = this.Page.FindControl("ObjectDataSource1") as ObjectDataSource;

            //DataSourceSelectArguments dd = new DataSourceSelectArguments();
            ////DataSourceSelectArguments dssa = new DataSourceSelectArguments(4, 3);

            //int ii1 = dd.StartRowIndex;
            //int ii2 = dd.TotalRowCount;

            //int jj1 = SelectArguments.StartRowIndex;    // GridView �ҩl�^��������
            //int jj2 = SelectArguments.MaximumRows;      // GridView �C����ܪ�����
            //bool jj3 = SelectArguments.RetrieveTotalRowCount;
            //string jj4 = SelectArguments.SortExpression;
            //int jj5 = SelectArguments.TotalRowCount;

            if (ods != null)
            {
                //ViewState["pp2"] = "ods is not null";
                ods.Selected += new ObjectDataSourceStatusEventHandler(ods_Selected);
            }
            //else
            //    ViewState["pp2"] = "ods is null";

            base.OnLoad(e);
        }

        /// �p���`�O����
        /// <summary>
        /// �q�귽�ӷ����o�u�`���ơv�C
        /// </summary>
        private void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.ReturnValue is IListSource)
            {
                _recordCount = ((IListSource)e.ReturnValue).GetList().Count;
                //ViewState["ppppp"] = _recordCount;
            }

            // �Y�b code-behind�A�i�o�ˮ���u�`���ơv
            //int intTotalNum = ((DataSet)e.ReturnValue).Tables[0].Rows.Count;
            //if ((intTotalNum > 0))
            //{
            //    lblTotalNum.Text = "�@ " + intTotalNum + " ��";
            //}
            //else
            //{
            //    lblTotalNum.Text = "�@ 0 ��";
            //}
        }



        // �O�_�n�ҥΥ[�j�����u�����v�\�� (�w�]���ҥ�)
        /// <summary>
        /// �O�_�n�ҥΥ[�j�����u�����v�\�� (�w�]���ҥ�)�C
        /// </summary>
        [System.ComponentModel.DefaultValue(""),
        System.ComponentModel.Category("Wizard - Appearance"),
        System.ComponentModel.Description("�O�_�n�ҥΥ[�j�����u�����v�\�� (�w�]���ҥ�)�C")]
        public bool WizardCustomPager
        {
            // �Y�e�� .aspx ���]�w WizardGridView �� WizardCustomPager �ݩ� (�O�_�n�ҥΤ���)�A�h�w�]�� true (�ҥ�)
            get { return (bool?)ViewState["WizardCustomPager"] ?? true; }
            set { ViewState["WizardCustomPager"] = value; }
        }


        // ����-UI
        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            if (WizardCustomPager)
                CreateCustomPager(row, columnSpan, pagedDataSource);
            else
                base.InitializePager(row, columnSpan, pagedDataSource);
        }


        // ����-UI (�C���n��ܴX���A�N�|�i�J�o�Ө�ƴX��)
        /// <summary>
        /// �إ߭��X�C�B�p�����ܤ���������T����ơC
        /// </summary>
        protected virtual void CreateCustomPager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            int pageCount = pagedDataSource.PageCount;              // �����`�� (�z�L SP�A���i���o���T�`����)
            int pageIndex = pagedDataSource.CurrentPageIndex + 1;   // �������� (�ثe�Ҧb����)
            int pageButtonCount = PagerSettings.PageButtonCount;    // ��������ܪ��������s�ƥ� (1)

            _recordCount = pagedDataSource.DataSourceCount;     // ���o ObjectDataSource �̼��o�� �`���ơA�������@���ܼ� (�i�qSP���o���T�`����)

            TableCell cell = new TableCell();
            row.Cells.Add(cell);
            if (columnSpan > 1) cell.ColumnSpan = columnSpan;
            //cell.ColumnSpan = 15;

            HtmlGenericControl pager = new HtmlGenericControl("div");
            pager.Attributes["class"] = "pagination";
            cell.Controls.Add(pager);

            if (_recordCount.HasValue)
            {
                int intPageLowerRecord = pageIndex * PageSize + 1 - PageSize;   // �����Ĥ@��������
                int intPageUpperRecord = pageIndex * PageSize;                  // �����̫�@��������

                // �ˬd GridView �̫�@�������ơA�O�_���s�Y�A��έ�n���� PageSize ���ƥ�
                int intLastPageRecord = (int)_recordCount % PageSize;

                // �Y�̫�@�������Ƭ��s�Y (���O��n�� PageSize ���ƥ�)�A�B�ϥΪ̤w�i��F�̫�@��
                if ((intLastPageRecord > 0) && (pageIndex == pageCount))
                {
                    intPageUpperRecord = pageIndex * PageSize - PageSize + intLastPageRecord;
                }

                // �ثe�g�k�A�N���X�C����Ʃ�����ܡA�H�קK�]���X�C��ƹL�h(�L��)�A�|�y�� GridView �������e�����|�o�ͱƪ����`���{�H
                cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>Total�G</span><span style='color:red;font-size:9pt'>" + _recordCount.ToString() + "</span>"));
                cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>  Present�G</span><span style='color:red;font-size:9pt'>" + intPageLowerRecord + " - " + intPageUpperRecord + "</span>"));
                cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>PageSize�G</span><span style='color:red;font-size:9pt'>" + PageSize.ToString() + "</span>"));
                //cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>  Page�G</span><span style='color:red;font-size:9pt'>" + pageIndex.ToString() + " / " + pageCount.ToString() + "</span>"));
                cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>PageCount�G</span><span style='color:red;font-size:9pt'>" + pageCount.ToString() + "</span>"));
                //cell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                cell.Controls.Add(new LiteralControl("<span style='font-size:9pt'>  Go to</span>"));

                HtmlInputText InputPage = new HtmlInputText();
                Button GoPage = new Button();
                GoPage.Height = 20;
                InputPage.ID = "txtPageIndex";
                InputPage.Style.Add(HtmlTextWriterStyle.Width, "20px");
                InputPage.Style.Add(HtmlTextWriterStyle.Height, "12px");
                cell.Controls.Add(InputPage);

                GoPage.ID = "GO";
                GoPage.Text = "GO";
                GoPage.Font.Size = System.Web.UI.WebControls.FontUnit.Smaller;
                GoPage.ToolTip = "Click here to go to the page number you want.";
                //GoPage.Visible = false;
                GoPage.Click += new System.EventHandler(OnGoPage_Click);
                cell.Controls.Add(GoPage);

                cell.ColumnSpan = this.Columns.Count;

                // �Y�ϥΪ̫����O Enter ��
                InputPage.Attributes.Add("onkeypress", "if(event.keyCode==13){ this.nextSibling.click();event.returnValue=false;}");

                // ���ؼg�k�A�i�N���X�C�����T���A�q���X�֦��@��C���Y���X�C��ƹL�h(�L��)�A�|�y�� GridView �������e�����|�o�ͱƪ����`���{�H
                //pager.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�`�@���ơG</span><span style='color:red;font-size:9pt'>" + _recordCount.ToString() + "</span>"));
                //pager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //pager.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�ثe���ơG</span><span style='color:red;font-size:9pt'>" + intPageLowerRecord + " - " + intPageUpperRecord + "</span>"));
                //pager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //pager.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�C�����ơG</span><span style='color:red;font-size:9pt'>" + PageSize.ToString() + "</span>"));
                //pager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //pager.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�ثe���ơG</span><span style='color:red;font-size:9pt'>" + pageIndex.ToString() + "</span>"));
                //pager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                //pager.Controls.Add(new LiteralControl("<span style='font-size:9pt'>�`���ơG</span><span style='color:red;font-size:9pt'>" + pageCount.ToString() + "</span>"));
                pager.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
            }

            if (pageCount >= 0)
            {
                //TableCell cell2 = new TableCell();
                //row.Cells.Add(cell2);

                //HtmlGenericControl pager = new HtmlGenericControl("div");
                //pager.Attributes["class"] = "pagination";
                //cell.Controls.Add(pager);

                int min = pageIndex - pageButtonCount;
                int max = pageIndex + pageButtonCount;

                if (max > pageCount)
                    min -= max - pageCount;
                else if (min < 1)
                    max += 1 - min;

                Control page = null;

                // �إߡu�Ĥ@���vLinkButton
                page = pageIndex > 1
                                ? BuildLinkButton(pageIndex - 2, PagerSettings.FirstPageText, "Page", "First")
                                : BuildSpan(PagerSettings.FirstPageText, "disabled");
                //pager.Controls.Add(page); // �o�� LinkButton �Y�]�[�J�A�b GridView �����ɴ����|�ɭP�ƪ������`�B���e�׶��ܡB�L�k����e��

                // �إߡu�W�@���vLinkButton
                page = pageIndex > 1
                                ? BuildLinkButton(pageIndex - 2, PagerSettings.PreviousPageText, "Page", "Prev")
                                : BuildSpan(PagerSettings.PreviousPageText, "disabled");
                pager.Controls.Add(page);


                // �إߡu���X�vLinkButton
                bool needDiv = false;
                for (int i = 1; i <= pageCount; i++)
                {
                    if (i <= 2 || i > pageCount - 2 || (min <= i && i <= max))
                    {
                        string text = i.ToString(NumberFormatInfo.InvariantInfo);
                        page = i == pageIndex
                                ? BuildSpan(text, "current")
                                : BuildLinkButton(i - 1, text, "Page", text);
                        pager.Controls.Add(page);
                        needDiv = true;
                    }
                    else if (needDiv)
                    {
                        page = BuildSpan("&hellip;", null);
                        pager.Controls.Add(page);
                        needDiv = false;
                    }
                }


                // �إߡu�U�@���vLinkButton
                page = pageIndex < pageCount
                        ? BuildLinkButton(pageIndex, PagerSettings.NextPageText, "Page", "Next")
                        : BuildSpan(PagerSettings.NextPageText, "disabled");
                pager.Controls.Add(page);


                // �إߡu�̫�@���vLinkButton
                page = pageIndex < pageCount
                                ? BuildLinkButton(pageIndex, PagerSettings.LastPageText, "Page", "Last")
                                : BuildSpan(PagerSettings.LastPageText, "disabled");
                //pager.Controls.Add(page); // �o�� LinkButton �Y�]�[�J�A�b GridView �����ɴ����|�ɭP�ƪ������`�B���e�׶��ܡB�L�k����e��


                // �g���������s���~�[�B�S�ʡA�����}�o�H���ѫe�� .aspx �ۭq
                PagerSettings.FirstPageText = "First";
                PagerSettings.PreviousPageText = "Previous";
                PagerSettings.NextPageText = "Next";
                PagerSettings.LastPageText = "Last";

                // �]�w�u���X�C (PagerRow)�v�A�n��ܦb GridView �W���٤U��A��ΤW�B�U�賣��ܡF
                // �åi�]�w�u���X�C�v�n��ܦb���B���B�٬O�k���C
                // �Y�b���g���A�h�}�o�H���Y�L�k�ѫe�ݦۭq
                PagerSettings.Position = PagerPosition.Top; //.TopAndBottom;
                PagerStyle.HorizontalAlign = HorizontalAlign.Right;

                PagerSettings.PageButtonCount = 2;
            }

        } // end of CreateCustomPager()


        // ����-UI
        private Control BuildLinkButton(int pageIndex, string text, string commandName, string commandArgument)
        {
            //LinkButton link = new LinkButton();
            WizardPagerLinkButton link = new WizardPagerLinkButton(this);
            link.Text = text;

            // 2008/09/16 �s�W�A�ۭq�u���X�C (�W�@���B1�B2�B�K�B�U�@��)�v���r��j�p
            //link.Font.Size = FontUnit.Smaller;  // FontSize.Smaller;

            link.EnableCallback(ParentBuildCallbackArgument(pageIndex));
            link.CommandName = commandName;
            link.CommandArgument = commandArgument;
            return link;
        }


        // ����-UI
        private Control BuildSpan(string text, string cssClass)
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            if (!String.IsNullOrEmpty(cssClass)) span.Attributes["class"] = cssClass;
            span.InnerHtml = text;
            return span;
        }


        // ����-UI
        private string ParentBuildCallbackArgument(int pageIndex)
        {
            MethodInfo m =
                typeof(GridView).GetMethod("BuildCallbackArgument", BindingFlags.NonPublic | BindingFlags.Instance, null,
                                            new Type[] { typeof(int) }, null);
            return (string)m.Invoke(this, new object[] { pageIndex });
        }

    } // end of class
} // end of namespace

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

WizardWu
Web Developer
Taiwan Taiwan
A young programmer in Taipei City, Taiwan. Earn little money and just coding for fun.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 7 Nov 2008
Article Copyright 2008 by WizardWu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid