c# - asp.net 中的分页

标签 c# asp.net paging

您好,我有一个包含 100000 行数据的表。现在我想在页面大小为 50 的用户表单中显示我的数据。

呈现它的最佳方法是什么。我应该选择数据表吗?或者我可以实现自己的选择查询,以便在每次按下下一步按钮时获取 50 条记录吗?

提前致谢

最佳答案

如果打开 AllowPaging 并将 PageSize 设置为 50,则可以使用 GridView 轻松完成此操作。但是它将是 < em>可怕的低效 - 每次你移动到一个新页面时,它都会读取所有 1 000 000 行,计算出它需要显示的 50 行,然后丢弃其余的。

您想要的是数据库中的存储过程,它获取要显示的页码,计算出该页面上的行集并将它们返回到 ASP.NET 页面。如果您使用的是 SQL Server 2005 或更高版本,您最好的选择是使用公用表表达式,因此您的存储过程看起来像这样(这是针对 Northwind 数据库的):

CREATE PROC [dbo].[PagedOrderList]
@PageNumber INTEGER
AS
SET NOCOUNT ON

DECLARE @lowerRecordNumber INTEGER  
DECLARE @upperRecordNumber INTEGER

-- Work out the record numbers that bound the page
SET @lowerRecordNumber = ((@pageNumber - 1) * 50) + 1
SET @upperRecordNumber = (@pageNumber * 50);

-- Create a CTE with all the records numbered
WITH OrdersCTE ([RowNumber],[OrderId],[OrderDate],[RequiredDate],[ShippedDate],  
[CompanyName],[Value])
AS
(
    SELECT
    ROW_NUMBER() OVER(ORDER BY o.[OrderId]),
    o.OrderID,
    o.OrderDate,
    o.RequiredDate,
    o.ShippedDate,
    c.CompanyName,
    SUM(od.Quantity * od.UnitPrice) AS [Value]
    FROM
    Orders o INNER JOIN [Order Details] od ON o.OrderID = od.OrderID
    INNER JOIN Customers c ON o.CustomerID = c.CustomerID
    GROUP BY o.OrderID, o.OrderDate, o.RequiredDate, o.ShippedDate, c.CompanyName
)
-- Select the rows from the CTE that fall between the bounds we worked out
SELECT * 
FROM OrdersCTE
WHERE [RowNumber] BETWEEN @lowerRecordNumber AND @upperRecordNumber 

现在,回到您的页面。您需要放入 DataGrid - 它们比其他任何东西都更好地支持自定义分页 - 并将 AllowCustomPaging 设置为 True。您可能会发现使用一种使用页码调用存储过程的方法更容易,然后您可以添加 Previous、Next、First、Last、+10、-10 按钮 - 无论您想要什么,只需计算出页码并通过它的方法。

 Private Sub loadData(ByVal pageNumber As Integer)

    Dim orderDataTable As DataTable
    'This uses the Microsoft Enterprise Library for data access
    Dim DAL As Database
    Dim cmd As DbCommand

    DAL = DatabaseFactory.CreateDatabase("Northwind")

    cmd = DAL.GetStoredProcCommand("PagedOrderList")

    'Pass the page number to the stored proc
    DAL.AddInParameter(cmd, "@pageNumber", DbType.Int32, pageNumber)

    'Get a DataTable back with the 50 rows we want
    orderDataTable = DAL.ExecuteDataSet(cmd).Tables(0)

    'Bind the data to the grid
    With OrderDataGrid
        .DataSource = orderDataTable
        .DataBind()
        'Set the page number so we know where we are in the dataset
        .CurrentPageIndex = pageNumber
    End With

End Sub


Private Sub PreviousButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PreviousButton.Click

    If OrderDataGrid.CurrentPageIndex = 0 Then
        'Make sure we don't try to load a negative page number
    Else
        'Work out the previous page number and load the data for it
        Call loadData(OrderDataGrid.CurrentPageIndex - 1)
    End If

End Sub

Private Sub NextButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NextButton.Click

    'Work out the nextpage number and load the data for it
    Call loadData(OrderDataGrid.CurrentPageIndex + 1)

End Sub

关于c# - asp.net 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1226646/

相关文章:

c# - 递归 HTTP 调用在 IDE 中表现出不同的行为与已部署的可执行文件

c# - 在不知道确切结构的情况下计算大量嵌套的 XmlNodes

javascript - 从 javascript 调用 WCF 方法

c# - asp updatepanel 中的文本框焦点 - 使用断点但并非没有

c# - 如何将枚举绑定(bind)到 ASP.Net 中的网格控件

c# - 在 IronPython 和 IronRuby 中打包脚本源文件

c# - 访问 LoginView 中的标签

docker - 同一主机上的 Docker 容器是否共享相同的页面缓存?

ios - Three20 TTThumbviewcontroller 分页加载更多链接不添加更多行

php - 根据查询的总结果检测页数的问题