c# - 没有数据源控制的高效 Gridview 分页问题

标签 c# .net asp.net gridview pagination

我正在尝试在不使用数据源控件的情况下使用 gridview 进行高效分页。高效是指我只检索我打算显示的记录。

我正在尝试使用 PagerTemplate 来构建我的寻呼机功能。

简而言之,问题是如果我只绑定(bind)我打算在当前页面上显示的记录,则 gridview 不会呈现其分页模板,因此我无法获得分页控件。

这几乎就好像我必须绑定(bind)比我打算在给定页面上显示的记录更多的记录,这不是我想做的事情。

最佳答案

您需要创建一个继承自 GridView 的自定义 gridview 控件。没有 DataSourceControl,gridview 不知道可能绑定(bind)到控件的记录总数。如果绑定(bind) 100 条记录中的 10 条并将 PageSize 属性设置为 10,则 gridview 仅知道有 10 条记录将小于或等于 PageSize,并且分页控件将不会显示。为了让您的 gridview 显示寻呼机,它必须知道可能被检索的记录总数。通过继承gridview,重写InitializePager方法,我们可以拦截pagedDataSource,修改AllowCustomPaging和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;
            }
        }
    }
}

然后当你绑定(bind)数据时:

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

关于c# - 没有数据源控制的高效 Gridview 分页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2518968/

相关文章:

.net - XmlNode 上的 SelectSingleNode 方法返回未定义的值

asp.net - IsPostBack、IsAsync 和 IsCallback 之间有什么区别?

c# - 调用 SSL 支付网关时出错 : "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel"

c# - 防止外部站点访问我的 ashx 页面

c# - bool 属性到 Func<bool> 的转换

c# - 创建一个代表 "today"的 NodaTime LocalDate

javascript - 通过 Azure API 管理流式传输

c# - 使用自定义控件在 silverlight 中设置样式与内联属性

c# - 系统.Data.SqlClient.SqlException : Column name or number of supplied values does not match table definition

c# - 从 MSMQ 读取时无法反序列化 Message.Body