Bagikan melalui


ObjectDataSource.EnablePaging Properti

Definisi

Mendapatkan atau menetapkan nilai yang menunjukkan apakah kontrol sumber data mendukung paging melalui kumpulan data yang diambilnya.

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

Nilai Properti

true jika kontrol sumber data mendukung paging melalui data yang diambilnya; jika tidak, false.

Contoh

Tiga contoh berikut menunjukkan halaman Web, kelas halaman di belakang kode, dan kelas akses data yang memungkinkan pengguna untuk memilih berapa banyak rekaman yang ditampilkan di halaman.

Halaman Web berisi kontrol yang ObjectDataSource propertinya EnablePaging diatur ke true. Properti SelectCountMethod diatur ke nama metode yang mengembalikan jumlah total rekaman dalam kueri. MaximumRowsParameterName Properti dan StartRowIndexParameterName properti diatur ke nama parameter yang digunakan dalam metode Pilih. Halaman ini juga berisi DropDownList kontrol.

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

Contoh kedua menunjukkan handler untuk ListControl.SelectedIndexChanged peristiwa DropDownList kontrol. Kode dalam handler mengatur PageSize properti ke pilihan pengguna.

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

Contoh ketiga menunjukkan kelas akses data yang mengambil data dari tabel Pelanggan. Ini termasuk metode bernama GetSubsetOfEmployees, yang ditetapkan ke SelectMethod properti ObjectDataSource kontrol. Contohnya juga menyertakan metode bernama GetEmployeeCount, yang ditetapkan ke SelectCountMethod properti ObjectDataSource kontrol. Kelas menggunakan LINQ untuk mengkueri tabel Pelanggan. Contohnya memerlukan kelas LINQ ke SQL yang mewakili database Northwind dan tabel Pelanggan. Untuk informasi selengkapnya, lihat Cara: Membuat LINQ ke Kelas SQL di Proyek Web.

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

Keterangan

Halaman menurut kontrol ditangani ObjectDataSource dengan mengatur EnablePagingproperti , , StartRowIndexParameterNameMaximumRowsParameterName, dan SelectCountMethod dari ObjectDataSource dan menentukan metode pilih dalam objek bisnis dengan parameter yang tepat. EnablePaging Saat properti diatur ke true, SelectParameters koleksi menyertakan dua parameter tambahan untuk baris pertama yang diminta dan jumlah baris yang diminta. Kedua parameter ini dinamai sebagaimana didefinisikan oleh StartRowIndexParameterName properti dan MaximumRowsParameterName . Metode Select harus mengembalikan jumlah baris yang diminta, dimulai dari indeks yang ditentukan. Karena data mungkin tidak dibagi secara merata berdasarkan ukuran halaman, halaman terakhir mungkin berisi lebih sedikit baris. Dengan demikian, jumlah baris yang diminta sebenarnya adalah jumlah maksimum baris yang dikembalikan.

Saat penomoran diaktifkan pada kontrol terikat data terkait, kontrol terikat data memanggil Select metode dengan indeks awal dan jumlah baris yang diperlukan. Selain itu, jika SelectCountMethod properti diatur, kontrol terikat data memanggil metode sebelum merender kontrol pager. Misalnya, jika GridView kontrol mengaktifkan halaman dengan ukuran halaman 5, dan metode yang ditentukan oleh SelectCountMethod properti mengembalikan 20, hanya 4 halaman yang ditampilkan di pager.

Properti EnablePaging mendelegasikan ke EnablePaging properti ObjectDataSourceView objek .

Berlaku untuk

Lihat juga