In Part II, I showed you how to extend the GridView control so that we can bind a generic list to it. Because GridViewEx inherits from GridView, it has all the functionalities GridView has (i.e. paging). Sometimes, it is desirable to change the number of items displayed on a page dynamically, to accommodate this, we will create a GridViewPager control (ascx).

3. GridViewPager

 Here is the code for GridViewPager:

    public partial class GridViewPager : System.Web.UI.UserControl
    {
        public delegate void SelectedIndexChangedHandler(object sender, int pageSize);
        public event SelectedIndexChangedHandler SelectedIndexChanged;
 
        private bool _visible = false;
        private int _pageSize = 10;
 
        public override bool Visible
        {
            get
            {
                if (ViewState[UniqueID] == null)
                    _visible = true;
                else
                    bool.TryParse(ViewState[this.UniqueID].ToString(), out _visible);
 
                return _visible;
            }
            set
            {
                _visible = value;
                ViewState[UniqueID] = _visible;
 
                if (_visible)
                    MainPanel.Visible = true;
                else
                    MainPanel.Visible = false;
            }
        }
 
        public int PageSize
        {
            get
            {
                if (ViewState[string.Concat(UniqueID, "PageSize")] == null)
                    _pageSize = int.Parse(DropDownItemsPerPage.SelectedValue);
                else
                    _pageSize = int.Parse(ViewState[string.Concat(UniqueID, "PageSize")].ToString());
 
                return _pageSize;
            }
            set
            {
                _pageSize = value;
                ViewState[String.Concat(UniqueID, "PageSize")] = value;
            }
        }
 
        protected override void OnInit(EventArgs e)
        {
            DropDownItemsPerPage.SelectedIndexChanged += new EventHandler(DropDownItemsPerPage_SelectedIndexChanged);
            base.OnInit(e);
        }
 
        protected void DropDownItemsPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageSize = int.Parse(DropDownItemsPerPage.SelectedValue);
 
            if (SelectedIndexChanged != null)
                SelectedIndexChanged(sender, PageSize);
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                DropDownItemsPerPage.DataBind();
        }
 
        /// <summary>
        /// This method links to the DataSourceChangedEvent defined in GridViewEx so that
        /// we can decide whether or not to show the pager depending on whether the list
        /// to be bound is empty.
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="isEmpty">whether the list to be bound is empty</param>
        public void GridViewDataSourceChanged(object sender, bool isEmpty)
        {
            if (isEmpty)
                Visible = false;
            else
                Visible = true;
        }
    }

As you can see, the pager control is quite simple. It communicates with GridViewEx control by the SelectedIndexChanged event. Depending on whether the state of number of items per page needs to persist between post backs, either ViewState or SessionState can be used.

See also

Code Download: ExtendingTheGridView.zip

Be Sociable, Share!