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

标签 asp.net gridview paging .net-4.5

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

ASP.NET 4.5 GridView.AllowCustomPaging 属性如何使此过程变得更简单?

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

最佳答案

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

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

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

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

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

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

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

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属性(如 here 中所述),但据我所知,totalRowCount 输出参数消除了 VirtualItemCount 属性。

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

因此,我们在 ASP.NET 4.5 中实现了 GridView 自定义分页......但 GridView.AllowCustomPagingGridView.VirtualItemCount 无处可见!

关于asp.net - ASP.NET 4.5 GridView.AllowCustomPaging 属性如何使这变得更简单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926266/

相关文章:

c# - 从 ASP.NET 中的 POST 方法检索数据

javascript - 面板内表格的优势

dictionary - Flutter ExpansionTile child 作为 Gridview?

memory-management - 操作系统通常如何管理内核内存和页面处理?

c# - 如何通过 $.ajax() 发布到 ASP.NET WEB API?

c# - .Net 桌面应用程序和 Web 应用程序之间的技术区别是什么?

每个 team_id 的 ASP.NET MVC 授权和角色

c# - 可编辑的 WPF GridView 行

c# - ASP.NET GridView : Get the PageIndex from the selected Row

ios - 在 UIWindow 上设置 rootViewController 会改变 UIScrollView 的分页布局