简体   繁体   中英

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. 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? I cant use third party controls or ajax.

UPDATE I cant use SQL to get 10 records at a time as well. 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.

Basically, you pass the sproc the number of items on the page and what page you are on. For example, page 3 and 20 records per page.

If you can use SQL Server 2005 or higher, you can use some newer keywords that make the query easier.

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.

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.

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. Create a class that will call the stored procedure and wire it to the grid using an ObjectDataSource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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