3

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

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

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

4

1 回答 1

5

根据我最近的经验,它没有。

具体来说:在使用 ASP.NET 4.5 的模型绑定系统时,在实现高效的 GridView 自定义分页(仅从一个非常大的数据库表中检索所需的数据页面)时,GridView.AllowCustomPaging属性没有进入它。

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

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

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

如果您不介意将 IQueryable 暴露给您的表示层,那么这是实现非常有效的分页的一种非常快速的方法。在运行时,框架会根据 GridView和属性自动将Skip() 和 Take()方法应用于您的源。仅从数据库返回所需的结果页面。简单的!PageIndexPageSize

(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属性(如此所述),但据我所知,totalRowCountout-parameter 排除了该VirtualItemCount属性。

如果(1)(2)未实现,则 GridView 将抛出异常:
当 DataBoundControl 启用分页时,SelectMethod 应返回 IQueryable 或应具有所有这些强制参数:int startRowIndex、int maximumRows、out整数总行数

所以,我们已经在 ASP.NET 4.5 中实现了 GridView 自定义分页......但是却GridView.AllowCustomPaging无处GridView.VirtualItemCount可寻!

于 2012-12-27T10:25:46.167 回答