ObjectDataSource.EnablePaging 屬性

定義

取得或設定值,指出資料來源控制項是否支援對其擷取的資料集進行分頁。Gets or sets a value that indicates whether the data source control supports paging through the set of data that it retrieves.

public:
 property bool EnablePaging { bool get(); void set(bool value); };
public bool EnablePaging { get; set; }
member this.EnablePaging : bool with get, set
Public Property EnablePaging As Boolean

屬性值

如果資料來源控制項支援對其擷取的資料進行分頁則為 true,否則為 falsetrue if the data source control supports paging through the data it retrieves; otherwise, false.

範例

下列三個範例顯示網頁、程式碼後置頁面類別, 以及可讓使用者選擇要在頁面中顯示多少筆記錄的資料存取類別。The following three examples show a Web page, a code-behind page class, and a data-access class that enable the user to pick how many records are displayed in the page.

網頁包含ObjectDataSourceEnablePaging屬性設定為true的控制項。The Web page contains an ObjectDataSource control whose EnablePaging property is set to true. SelectCountMethod屬性會設定為傳回查詢中記錄總數之方法的名稱。The SelectCountMethod property is set to the name of a method that returns the total number of records in the query. MaximumRowsParameterName 屬性StartRowIndexParameterName和屬性會設定為 Select 方法中所使用之參數的名稱。The MaximumRowsParameterName property and the StartRowIndexParameterName property are set to the names of the parameters that are used in the Select method. 此頁面也包含DropDownList控制項。The page also contains a DropDownList control.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ObjectDataSource Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    How many rows to display on this page:<br />
    <asp:DropDownList 
          AutoPostBack="true" 
          ID="rowsToDisplay" 
          runat="server" 
          onselectedindexchanged="rowsToDisplay_SelectedIndexChanged">
        <asp:ListItem Value="5"></asp:ListItem>
        <asp:ListItem Value="10" Selected="True"></asp:ListItem>
        <asp:ListItem Value="20"></asp:ListItem>
    </asp:DropDownList> 
    
    <asp:ObjectDataSource 
        SelectCountMethod="GetEmployeeCount" 
        EnablePaging="true" 
        TypeName="CustomerLogic" 
        SelectMethod="GetSubsetOfEmployees"
        MaximumRowsParameterName="maxRows"
        StartRowIndexParameterName="startRows"
        ID="ObjectDataSource1" 
        runat="server">
    </asp:ObjectDataSource>
    
    <asp:GridView 
        DataSourceID="ObjectDataSource1" 
        AllowPaging="true" 
        ID="GridView1" 
        runat="server">
    </asp:GridView>
    
    </div>
    </form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    How many rows to display on this page:<br />
    <asp:DropDownList 
          AutoPostBack="true" 
          ID="rowsToDisplay" 
          runat="server" 
          onselectedindexchanged="rowsToDisplay_SelectedIndexChanged">
        <asp:ListItem Value="5"></asp:ListItem>
        <asp:ListItem Value="10" Selected="True"></asp:ListItem>
        <asp:ListItem Value="20"></asp:ListItem>
    </asp:DropDownList> 
    
    <asp:ObjectDataSource 
        SelectCountMethod="GetEmployeeCount" 
        EnablePaging="true" 
        TypeName="CustomerLogic" 
        SelectMethod="GetSubsetOfEmployees"
        MaximumRowsParameterName="maxRows"
        StartRowIndexParameterName="startRows"
        ID="ObjectDataSource1" 
        runat="server">
    </asp:ObjectDataSource>
    
    <asp:GridView 
        DataSourceID="ObjectDataSource1" 
        AllowPaging="true" 
        ID="GridView1" 
        runat="server">
    </asp:GridView>
    
    </div>
    </form>
</body>
</html>

第二個範例顯示ListControl.SelectedIndexChanged DropDownList控制項之事件的處理常式。The second example shows a handler for the ListControl.SelectedIndexChanged event of the DropDownList control. 處理常式中的程式碼會PageSize將屬性設定為使用者的選取專案。The code in the handler sets the PageSize property to the user's selection.

protected void rowsToDisplay_SelectedIndexChanged(object sender, EventArgs e)
{
    GridView1.PageSize = int.Parse(rowsToDisplay.SelectedValue);
}
Protected Sub rowsToDisplay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rowsToDisplay.SelectedIndexChanged
    GridView1.PageSize = Integer.Parse(rowsToDisplay.SelectedValue)
End Sub

第三個範例顯示從 Customers 資料表抓取資料的資料存取類別。The third example shows the data access class that retrieves data from the Customers table. 其中包含名為GetSubsetOfEmployees的方法, 它會指派SelectMethodObjectDataSource控制項的屬性。It includes a method named GetSubsetOfEmployees, which is assigned to the SelectMethod property of the ObjectDataSource control. 此範例也包含名為GetEmployeeCount的方法, 它會指派SelectCountMethodObjectDataSource控制項的屬性。The example also includes a method named GetEmployeeCount, which is assigned to the SelectCountMethod property of the ObjectDataSource control. 類別會使用 LINQ 查詢 Customers 資料表。The class uses LINQ to query the Customers table. 此範例需要一個代表 Northwind 資料庫和 Customers 資料表的 LINQ to SQL 類別。The example requires a LINQ to SQL class that represents the Northwind database and Customers table. 如需詳細資訊,請參閱如何:在 Web 專案中建立 LINQ to SQL 類別。For more information, see How to: Create LINQ to SQL Classes in a Web Project.

public class CustomerLogic
{

    public List<Customer> GetSubsetOfEmployees(int startRows, int maxRows)
    {
        NorthwindDataContext ndc = new NorthwindDataContext();
        var customerQuery = 
            from c in ndc.Customers
            select c;

        return customerQuery.Skip(startRows).Take(maxRows).ToList<Customer>();
    }

    public int GetEmployeeCount()
    {
        object cachedCount = HttpRuntime.Cache["TotalEmployeeCount"];
        if (cachedCount != null)
        {
            return int.Parse(cachedCount.ToString());
        }
        else
        {
            NorthwindDataContext ndc = new NorthwindDataContext();
            var totalNumberQuery =
                from c in ndc.Customers
                select c;
            
            int employeeCount = totalNumberQuery.Count();
            HttpRuntime.Cache.Add("TotalEmployeeCount", employeeCount, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            return employeeCount;
        }
    }
}
Public Class CustomerLogic
    Public Function GetSubsetOfEmployees(ByVal startRows As Integer, ByVal maxRows As Integer) As List(Of Customer)

        Dim ndc As New NorthwindDataContext()
        Dim customerQuery = _
        From c In ndc.Customers _
            Select c

        Return customerQuery.Skip(startRows).Take(maxRows).ToList()
    End Function

    Public Function GetEmployeeCount() As Integer

        Dim cachedCount = HttpRuntime.Cache("TotalEmployeeCount")
        If cachedCount IsNot Nothing Then
            Return Integer.Parse(cachedCount.ToString())
        Else
            Dim ndc As New NorthwindDataContext()
            Dim totalNumberQuery = _
            From c In ndc.Customers _
                Select c

            Dim employeeCount = totalNumberQuery.Count()
            HttpRuntime.Cache.Add("TotalEmployeeCount", employeeCount, Nothing, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, Nothing)
            Return employeeCount
        End If
    End Function
End Class

備註

ObjectDataSourceStartRowIndexParameterName設定的、、MaximumRowsParameterNameSelectCountMethod 屬性ObjectDataSource , 並在具有適當參數的商務物件中定義 select 方法, 來處理控制項的分頁。 EnablePagingPaging by the ObjectDataSource control is handled by setting the EnablePaging, StartRowIndexParameterName, MaximumRowsParameterName, and SelectCountMethod properties of the ObjectDataSource and defining a select method in the business object with the proper parameters. 當屬性設定為true時, SelectParameters集合會包含所要求之第一個資料列的兩個額外參數, 以及所要求的資料列數目。 EnablePagingWhen the EnablePaging property is set to true, the SelectParameters collection includes two additional parameters for the first row that is requested and the number of rows that are requested. 這兩個參數的命名方式如StartRowIndexParameterNameMaximumRowsParameterName屬性所定義。These two parameters are named as defined by the StartRowIndexParameterName and MaximumRowsParameterName properties. Select方法應該會傳回要求的資料列數目, 從指定的索引開始。The Select method should return the requested number of rows, starting at the specified index. 因為資料可能不會平均除以頁面大小, 所以最後一頁可能會包含較少的資料列。Because the data might not divide evenly by the page size, the last page might contain fewer rows. 因此, 所要求的資料列數目實際上是傳回的最大資料列數目。Thus, the number of rows that are requested is actually the maximum number of rows that are returned.

在相關聯的資料繫結控制項上啟用分頁時, 資料繫結控制項會使用開始索引和Select所需的資料列數目來呼叫方法。When paging is enabled on the associated data-bound control, the data-bound control calls the Select method with the start index and number of rows that are required. 此外, 如果SelectCountMethod已設定屬性, 則資料繫結控制項會在轉譯呼機控制項之前呼叫方法。Additionally, if the SelectCountMethod property is set, the data-bound control calls the method before rendering the pager controls. 例如, 如果GridView控制項已啟用頁面大小為5的分頁, 而SelectCountMethod屬性所指定的方法傳回 20, 則分頁程式中只會顯示4個頁面。For example, if a GridView control has paging enabled with a page size of 5, and the method that is specified by the SelectCountMethod property returns 20, only 4 pages are displayed in the pager.

屬性會委派給ObjectDataSourceView物件EnablePaging的屬性。 EnablePagingThe EnablePaging property delegates to the EnablePaging property of the ObjectDataSourceView object.

適用於

另請參閱