代码之家  ›  专栏  ›  技术社区  ›  Ronnie Overby

没有数据源控件的高效gridview分页问题

  •  1
  • Ronnie Overby  · 技术社区  · 14 年前

    我试图在不使用数据源控件的情况下使用gridview进行有效的分页。说到效率,我是说我只检索我想要显示的记录。

    我正试图使用pagertemplate来构建我的寻呼机功能。

    简而言之,问题是如果我只绑定要在当前页面上显示的记录,gridview就不会呈现它的寻呼机模板,所以我就无法获得分页控件。

    这就好像我要绑定的记录比我在给定页面上显示的要多,这不是我想做的事情。

    1 回复  |  直到 11 年前
        1
  •  7
  •   Community WizardZ    12 年前

    您需要创建一个从gridview继承的自定义gridview控件。如果没有datasourcecontrol,gridview就不知道可能绑定到控件的记录总数。如果绑定100条记录中的10条,并将pagesize属性设置为10,则gridview只知道有10条记录将小于或等于pagesize,而pager控件将不会显示。为了让gridview显示寻呼机,它必须知道可能检索到的记录总数。通过继承GridView并重写InitializePager方法,我们可以拦截PagedDataSource并修改AllowCustoming和VirtualCount方法。

    这是我创造的

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    
    namespace cly.Web.CustomControls
    {
        public class clyGridView : GridView
        {
            private const string _virtualCountItem = "bg_vitemCount";
            private const string _sortColumn = "bg_sortColumn";
            private const string _sortDirection = "bg_sortDirection";
            private const string _currentPageIndex = "bg_pageIndex";
    
            public clyGridView ()
                : base()
            {
            }
    
            #region Custom Properties
            [Browsable(true), Category("NewDynamic")]
            [Description("Set the virtual item count for this grid")]
            public int VirtualItemCount
            {
                get
                {
                    if (ViewState[_virtualCountItem] == null)
                        ViewState[_virtualCountItem] = -1;
                    return Convert.ToInt32(ViewState[_virtualCountItem]);
                }
                set
                {
                    ViewState[_virtualCountItem] = value;
                }
            }        
    
            public string GridViewSortColumn
            {
                get
                {
                    if (ViewState[_sortColumn] == null)
                        ViewState[_sortColumn] = string.Empty;
                    return ViewState[_sortColumn].ToString();
                }
                set
                {
                    if (ViewState[_sortColumn] == null || !ViewState[_sortColumn].Equals(value))
                        GridViewSortDirection = SortDirection.Ascending;
                    ViewState[_sortColumn] = value;
                }
            }
    
            public SortDirection GridViewSortDirection
            {
                get
                {
                    if (ViewState[_sortDirection] == null)
                        ViewState[_sortDirection] = SortDirection.Ascending;
                    return (SortDirection)ViewState[_sortDirection];
                }
                set
                {
                    ViewState[_sortDirection] = value;
                }
            }
    
            private int CurrentPageIndex
            {
                get
                {
                    if (ViewState[_currentPageIndex] == null)
                        ViewState[_currentPageIndex] = 0;
                    return Convert.ToInt32(ViewState[_currentPageIndex]);
                }
                set
                {
                    ViewState[_currentPageIndex] = value;
                }
            }
    
            private bool CustomPaging
            {
                get { return (VirtualItemCount != -1); }
            }
            #endregion
    
            #region Overriding the parent methods
            public override object DataSource
            {
                get
                {
                    return base.DataSource;
                }
                set
                {
                    base.DataSource = value;
                    // store the page index so we don't lose it in the databind event
                    CurrentPageIndex = PageIndex;
                }
            }
    
            protected override void OnSorting(GridViewSortEventArgs e)
            {            
                //Store the direction to find out if next sort should be asc or desc
                SortDirection direction = SortDirection.Ascending;
                if (ViewState[_sortColumn] != null &&  (SortDirection)ViewState[_sortDirection] == SortDirection.Ascending)
                {
                    direction = SortDirection.Descending;
                }
                GridViewSortDirection = direction;
                GridViewSortColumn = e.SortExpression;
                base.OnSorting(e);
            }
    
            protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
            {
                // This method is called to initialise the pager on the grid. We intercepted this and override
                // the values of pagedDataSource to achieve the custom paging using the default pager supplied
                if (CustomPaging)
                {
                    pagedDataSource.VirtualCount = VirtualItemCount;
                    pagedDataSource.CurrentPageIndex = CurrentPageIndex;
                }
                base.InitializePager(row, columnSpan, pagedDataSource);
            }
    
            protected override object SaveViewState()
            {
                //object[] state = new object[3];
                //state[0] = base.SaveViewState();
                //state[1] = this.dirtyRows;
                //state[2] = this.newRows;
                //return state;
    
                return base.SaveViewState();
            }
    
            protected override void LoadViewState(object savedState)
            {
    
                //object[] state = null;
    
                //if (savedState != null)
                //{
                //    state = (object[])savedState;
                //    base.LoadViewState(state[0]);
                //    this.dirtyRows = (List<int>)state[1];
                //    this.newRows = (List<int>)state[2];
                //}
    
                base.LoadViewState(savedState);
            }
            #endregion
    
            public override string[] DataKeyNames
            {
                get
                {
                    return base.DataKeyNames;
                }
                set
                {
                    base.DataKeyNames = value;
                }
            }
    
            public override DataKeyArray DataKeys
            {
                get
                {
                    return base.DataKeys;
                }
            }
    
            public override DataKey SelectedDataKey
            {
                get
                {
                    return base.SelectedDataKey;
                }
            }
        }
    }
    

    然后在绑定数据时:

    gv.DataSource = yourListOrWhatever
    gv.VirtualItemCount = numOfTotalRecords;
    gv.DataBind();