简体   繁体   English

在asp.net中分页

[英]Paging in asp.net

Hi I've a table with 100000 rows of data. 嗨,我有一个表有100000行数据。 Now i want to present my data in user form with page size 50. 现在我要以页面大小为50的用户形式显示数据。

What is the best approach to present it. 呈现它的最佳方法是什么。 Shall i preffer datalist? 我应该提供数据列表吗? or can i implement my own select query for getting 50 records each time when i press next button? 还是我可以实现自己的选择查询,每次按next按钮时每次获取50条记录?

Thanks in advance 提前致谢

You could do this quite easily with a GridView if you switch on AllowPaging and set the PageSize to 50. But it will be horribly inefficient - every time you move to a new page it'll read all 1 000 000 rows, work out which 50 it needs to display, and throw the rest away. 你可以用一个GridView,如果你打开做到这一点很容易AllowPaging和设置PageSize为50。 但是,这将是可怕的效率低下-每次移动到一个新的页面,它会读取所有1个000 000行时间,制定出这50它需要显示,并将其余的扔掉。

What you want instead is a stored proc in your database that takes the page number that you want to display, works out the set of rows on that page and returns them to the ASP.NET page. 相反,您想要的是数据库中存储的proc,该proc带有您要显示的页码,计算出该页上的行集,然后将其返回到ASP.NET页。 If you're using SQL Server 2005 or later your best bet is to use a Common Table Expression, so your stored proc will look something like this (this is for the Northwind db): 如果您使用的是SQL Server 2005或更高版本,则最好的选择是使用通用表表达式,因此您存储的proc看起来将如下所示(这适用于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 

Now, back to your page. 现在,返回您的页面。 You'll need to put in a DataGrid - they have better support for custom paging than anything else - and set AllowCustomPaging to True. 您将需要放入一个DataGrid-它们对自定义分页的支持比其他任何方式都更好-并将AllowCustomPaging设置为True。 You might find it easier to have one method that calls your stored proc with the page number, then you can add Previous, Next, First, Last, +10, -10 buttons - whatever you want, just work out the page number and pass it to the method. 您可能会发现使用一种用页码调用存储的proc的方法会更容易,然后您可以添加上一页,下一页,第一页,最后一页,+ 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

I've created a paging control for asp.net. 我为asp.net创建了一个分页控件。 It's a basic control but it may help you. 这是基本控件,但可能会对您有所帮助。 Basic Pager Control 基本寻呼机控制

For 100000, it will be very time consuming to get all the records from the database into the dataset and then page them. 对于100000,将所有记录从数据库获取到数据集中然后分页将非常耗时。 Instead I would go with implementing paging in the database stored procedure/ query. 相反,我会在数据库存储过程/查询中实现分页。 That way only 50 records would be retrieved in the front end code at a time and user response would be faster. 这样,一次只能在前端代码中检索50条记录,并且用户响应会更快。

那“ ListView”和“ DataPager”呢?

I would use a pagedDataSource then you can bind to a repeater, datalist or whatever. 我将使用pagedDataSource,然后可以绑定到转发器,数据列表或其他任何内容。

there is and example here . 有和例子在这里

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

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