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

次の 3 つの例は、Web ページ、分離コード ページ クラス、およびユーザーがページに表示されるレコードの数を選択できるようにするデータ アクセス クラスを示しています。

Web ページには、 プロパティが ObjectDataSourcetrue設定されているコントロールEnablePagingが含まれています。 プロパティは SelectCountMethod 、クエリ内のレコードの合計数を返すメソッドの名前に設定されます。 プロパティと StartRowIndexParameterName プロパティはMaximumRowsParameterName、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>

2 番目の例は、 コントロールの イベントの ListControl.SelectedIndexChanged ハンドラーを DropDownList 示しています。 ハンドラーのコードは、 プロパティを 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

3 番目の例は、Customers テーブルからデータを取得するデータ アクセス クラスを示しています。 コントロールの プロパティに割り当てられる SelectMethod という名前GetSubsetOfEmployeesのメソッドがObjectDataSource含まれています。 この例には、 コントロールの プロパティにSelectCountMethod割り当てられる という名前GetEmployeeCountObjectDataSourceメソッドも含まれています。 クラスは 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ページングは、 の 、StartRowIndexParameterNameMaximumRowsParameterName、および プロパティObjectDataSourceEnablePaging設定しSelectCountMethod、ビジネス オブジェクトで適切なパラメーターを使用して select メソッドを定義することによって処理されます。 プロパティが EnablePagingtrue設定されている場合、 SelectParameters コレクションには、要求された最初の行と要求された行数の 2 つの追加パラメーターが含まれます。 これら 2 つのパラメーターの名前は、 プロパティと MaximumRowsParameterName プロパティで定義されていますStartRowIndexParameterName。 メソッドは Select 、指定したインデックスから開始して、要求された行数を返す必要があります。 データはページ サイズで均等に分割されない可能性があるため、最後のページの行数が少なくなる可能性があります。 したがって、要求される行の数は、実際には返される行の最大数です。

関連付けられているデータ バインド コントロールでページングが有効になっている場合、データ バインド コントロールは、開始インデックスと必要な行数を指定して メソッドを呼び出します Select 。 さらに、 プロパティが SelectCountMethod 設定されている場合、データ バインド コントロールは、ポケットベル コントロールをレンダリングする前に メソッドを呼び出します。 たとえば、ページ サイズが 5 のコントロールでページングが有効になっており、 プロパティでSelectCountMethod指定されたメソッドが 20 を返す場合GridView、ポケットベルには 4 ページのみが表示されます。

プロパティは EnablePaging 、 オブジェクトの EnablePaging プロパティに ObjectDataSourceView デリゲートします。

適用対象

こちらもご覧ください