简体   繁体   English

ASP.NET 2.0网格中的分页改进

[英]Pagination improvements in ASP.NET 2.0 Grids

I currently have a datagrid bound to a table with tens of thousands of records. 我目前有一个绑定到具有成千上万条记录的表的数据网格。 I display the datagrid using the existing asp.net 2.0. 我使用现有的asp.net 2.0显示datagrid。 I show ten records at a time. 我一次显示十条记录。

My problem is whenever I try to access the next page, I gets all the records again from the database and then displays the ones that are required. 我的问题是,每当我尝试访问下一页时,我都会再次从数据库中获取所有记录,然后显示所需的记录。 This is slowing down the app. 这会使应用程序变慢。 Is there feature within .net 2.0 which will help me optimize this issue? .net 2.0中是否有功能可以帮助我优化此问题? I cant use third party controls or ajax. 我不能使用第三方控件或ajax。

UPDATE I cant use SQL to get 10 records at a time as well. 更新我也不能使用SQL一次获取10条记录。 Without changing any existing business logic or data retrieval, I want to do this. 在不更改任何现有业务逻辑或数据检索的情况下,我想这样做。

You can do paging in your SQL and/or stored procedure. 您可以在SQL和/或存储过程中进行分页。

Basically, you pass the sproc the number of items on the page and what page you are on. 基本上,您会向sproc传递页面上的项目数以及您所在的页面。 For example, page 3 and 20 records per page. 例如,第3页和每页20条记录。

If you can use SQL Server 2005 or higher, you can use some newer keywords that make the query easier. 如果可以使用SQL Server 2005或更高版本,则可以使用一些较新的关键字来简化查询。

Here is a simple version of such a query: 这是此类查询的简单版本:

SELECT ClientName, RowNumber
FROM (SELECT ClientName, ROW_NUMBER() OVER (ORDER BY ClientName) AS RowNumber
FROM Clients) AS cl
WHERE RowNumber BETWEEN 12 AND 30

You would make the values above of "12" and "30" be input parameters. 您可以将“ 12”和“ 30”以上的值作为输入参数。

This way, you are only returning the rows you are going to display. 这样,您只返回要显示的行。

You can stick your datasource in the session. 您可以将数据源保留在会话中。

then check if your DataSource is null from the session and then go get it. 然后在会话中检查您的DataSource是否为null,然后去获取它。

This way you only get your data one time and if you need to get fresh data (say because you updated something) just empty the session variable that holds your data source. 这样,您仅一次获取数据,并且如果您需要获取新数据(例如,因为您更新了某些内容),只需清空保存数据源的会话变量。

EDIT: 编辑:

here's a half attempt at an example, (by this i mean I'm not going into how to change the page because I assume you already know how to do that) 这是一个示例的一半尝试,(这意味着我不打算更改页面,因为我假设您已经知道该怎么做)

protected void PageChanging(object sender, GridViewPageEventArgs e)
{
  if(Session["YourSessionVariableForData"] == null)
    Session["YourSessionVariableForData"] = YourDataCall();
  YourGridView.PageIndex = e.NewPageIndex;
  YourGridView.DataSource = ((YourDataType)Session["YourSessionVariableForData"]);
  YourGridView.DataBind();
}

Do the Paging in a Stored Procedure. 在存储过程中进行分页。 Look into using the ROW_NUMBER() function for this. 考虑使用ROW_NUMBER()函数。 Create a class that will call the stored procedure and wire it to the grid using an ObjectDataSource. 创建一个将调用存储过程的类,并使用ObjectDataSource将其连接到网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM