ASP.NET 4.5 GridView.AllowCustomPaging 属性如何使这个变得更简单?

问题描述 投票:0回答:1

我有一个 ASP.NET 4 GridView 控件,它使用这两篇文章中讨论的逻辑:

ASP.NET 4.5

GridView.AllowCustomPaging
属性如何使这个变得更简单?

非常欢迎提供有关如何使用它的文章的链接。

asp.net gridview pagination .net-4.5
1个回答
5
投票

根据我最近的经验,事实并非如此。

具体来说:当使用 ASP.NET 4.5 的 Model Binding 系统实现高效的 GridView 自定义分页(仅从非常大的数据库表中检索所需的数据页)时,

GridView.AllowCustomPaging
属性不会进入其中。

设置 GridView.SelectMethod 属性会导致使用模型绑定,这为我们提供了“ObjectDataSource 样式”功能(如链接中所述),而不需要 ObjectDataSource。 在这种方法中,有两种选择:

(1)

GridView.SelectMethod
指定的方法返回一个
IQueryable
,例如:

public IQueryable<MyClass> MySelectMethod1()
{
    return myService.GetAll(someCriteria);
}

如果您不介意将 IQueryable 暴露给表示层,那么这是实现非常高效的分页的一种非常快速的方法。 在运行时,框架根据 GridView 的 PageIndex

PageSize
属性,自动将
Skip() 和 Take()
方法应用于您的源。数据库仅返回所需的结果页。简单!

(2)

GridView.SelectMethod
指定的方法返回一些其他可绑定对象,例如:

public IList<MyClass> MySelectMethod2(int startRowIndex, int maximumRows, out int totalRowCount)
{
    totalRowCount = myService.GetCount(someCriteria);
    return myService.GetPage(someCriteria, startRowIndex, maximumRows);
}

通过设置totalRowCount,我们现在已经为GridView提供了正确呈现其Pager所需的所有信息,同时仅从数据库中检索了所需的数据页。

我本来希望使用

VirtualItemCount
属性(如此处所述),但据我所知,
totalRowCount
输出参数消除了
VirtualItemCount
属性。

如果 (1)(2) 没有实现,那么 GridView 将抛出异常:
当 DataBoundControl 启用分页时,SelectMethod 应该返回 IQueryable 或应该具有所有这些强制参数:int startRowIndex、int MaximumRows、out int TotalRowCount

因此,我们在 ASP.NET 4.5 中实现了 GridView 自定义分页......但是

GridView.AllowCustomPaging
GridView.VirtualItemCount
无处可见!

© www.soinside.com 2019 - 2024. All rights reserved.