ObjectDataSource.EnablePaging 屬性

定義

取得或設定值,指出資料來源控制項是否支援對其擷取的資料集進行分頁。

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,否則為 false

範例

下列三個範例顯示網頁、程式碼後置頁面類別,以及資料存取類別,可讓使用者挑選頁面中顯示的記錄數目。

網頁包含 ObjectDataSource 控制項,其 EnablePaging 屬性設定為 true 。 屬性 SelectCountMethod 會設定為方法的名稱,這個方法會傳回查詢中的記錄總數。 屬性 MaximumRowsParameterNameStartRowIndexParameterName 屬性會設定為 Select 方法中使用的參數名稱。 頁面也包含 DropDownList 控制項。

<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>

第二個範例顯示 控制項事件的 DropDownList 處理常式 ListControl.SelectedIndexChanged 。 處理常式中的程式碼會將 PageSize 屬性設定為使用者的選取範圍。

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 資料表擷取資料的資料存取類別。 它包含名為 GetSubsetOfEmployees 的方法,這個方法會指派給 SelectMethod 控制項的 ObjectDataSource 屬性。 此範例也包含名為 GetEmployeeCount 的方法,這個方法會指派給 SelectCountMethod 控制項的 ObjectDataSource 屬性。 類別會使用 LINQ 查詢 Customers 資料表。 此範例需要代表 Northwind 資料庫和 Customers 資料表的 LINQ to SQL 類別。 如需詳細資訊,請參閱如何:在 Web 專案中建立LINQ to SQL類別

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

備註

控制項的 ObjectDataSource 分頁是藉由設定 EnablePagingStartRowIndexParameterNameMaximumRowsParameterNameSelectCountMethod 屬性 ObjectDataSource 來處理,並使用適當的參數在商務物件中定義 select 方法。 EnablePaging當 屬性設定為 true 時, SelectParameters 集合會包含所要求第一個資料列的兩個額外參數,以及所要求的資料列數目。 這兩個參數的名稱是由 和 MaximumRowsParameterName 屬性所 StartRowIndexParameterName 定義。 方法 Select 應該會傳回要求的資料列數目,從指定的索引開始。 因為資料可能不會平均除以頁面大小,所以最後一頁可能包含較少的資料列。 因此,要求的資料列數目實際上是傳回的資料列數目上限。

在相關聯的資料繫結控制項上啟用分頁時,資料繫結控制項會呼叫 Select 方法,其中包含開始索引和所需的資料列數目。 此外,如果 SelectCountMethod 已設定 屬性,資料繫結控制項會在轉譯呼叫器控制項之前呼叫 方法。 例如,如果 GridView 控制項已啟用頁面大小為 5 的分頁,而 屬性所 SelectCountMethod 指定的 方法會傳回 20,則頁面巡覽器中只會顯示 4 個頁面。

屬性 EnablePaging 會委派給 EnablePaging 物件的 屬性 ObjectDataSourceView

適用於

另請參閱