Aracılığıyla paylaş


ObjectDataSource.EnablePaging Özellik

Tanım

Veri kaynağı denetiminin aldığı veri kümesinde disk belleğini destekleyip desteklemediğini belirten bir değer alır veya ayarlar.

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

Özellik Değeri

true veri kaynağı denetimi, alınan veriler üzerinden disk belleğini destekliyorsa; aksi takdirde , false.

Örnekler

Aşağıdaki üç örnekte bir Web sayfası, arka planda kod sayfası sınıfı ve kullanıcının sayfada kaç kayıt görüntüleneceğini seçmesini sağlayan bir veri erişim sınıfı gösterilir.

Web sayfası, özelliği olarak trueayarlanmış bir ObjectDataSource denetim EnablePaging içerir. SelectCountMethod özelliği, sorgudaki toplam kayıt sayısını döndüren bir yöntemin adına ayarlanır. MaximumRowsParameterName özelliği ve StartRowIndexParameterName özelliği, Select yönteminde kullanılan parametrelerin adlarına ayarlanır. Sayfa ayrıca bir DropDownList denetim içerir.

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

İkinci örnekte, denetimin ListControl.SelectedIndexChanged olayı için bir işleyici gösterilmektedir DropDownList . İşleyicideki kod, özelliği kullanıcının seçimine ayarlar 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

Üçüncü örnekte Müşteriler tablosundan veri alan veri erişim sınıfı gösterilmektedir. Denetimin özelliğine SelectMethodObjectDataSource atanan adlı GetSubsetOfEmployeesbir yöntem içerir. Örnek ayrıca, denetimin SelectCountMethod özelliğine ObjectDataSource atanan adlı GetEmployeeCountbir yöntem içerir. sınıfı, Müşteriler tablosunu sorgulamak için LINQ kullanır. Örnek, Northwind veritabanını ve Customers tablosunu temsil eden bir LINQ to SQL sınıfı gerektirir. Daha fazla bilgi için bkz . Nasıl yapılır: Web Projesinde LINQ to SQL Sınıfları Oluşturma.

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

Açıklamalar

Denetim tarafından disk belleği, öğesinin ObjectDataSource , StartRowIndexParameterName, MaximumRowsParameterNameve özellikleri ObjectDataSource ayarlanarak EnablePagingve SelectCountMethod iş nesnesinde uygun parametrelerle bir seçme yöntemi tanımlanarak işlenir. EnablePaging özelliği olarak trueSelectParameters ayarlandığında, koleksiyon istenen ilk satır ve istenen satır sayısı için iki ek parametre içerir. Bu iki parametre ve MaximumRowsParameterName özellikleri tarafından StartRowIndexParameterName tanımlandığı şekilde adlandırılır. yöntemi, Select belirtilen dizinden başlayarak istenen satır sayısını döndürmelidir. Veriler sayfa boyutuna eşit olarak bölünebileceğinden, son sayfada daha az satır bulunabilir. Bu nedenle, istenen satır sayısı aslında döndürülen en fazla satır sayısıdır.

İlişkili veriye bağlı denetimde disk belleği etkinleştirildiğinde, veriye bağlı denetim başlangıç dizini ve gerekli satır sayısıyla yöntemini çağırır Select . Ayrıca, özellik ayarlanırsa SelectCountMethod , veriye bağlı denetim çağrı denetimlerini işlemeden önce yöntemini çağırır. Örneğin, bir GridView denetimin disk belleği 5 sayfa boyutuyla etkinleştirildiyse ve özelliği tarafından SelectCountMethod belirtilen yöntem 20 döndürüyorsa, çağrı kutusunda yalnızca 4 sayfa görüntülenir.

EnablePaging özelliği, nesnesinin EnablePaging özelliğine temsilci atarObjectDataSourceView.

Şunlara uygulanır

Ayrıca bkz.