ObjectDataSource.EnablePaging Proprietà

Definizione

Ottiene o imposta un valore indicante se il controllo origine dati supporta il paging dell'insieme di dati recuperati.

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

Valore della proprietà

true, se il controllo origine dati supporta il paging dei dati recuperati; in caso contrario, false.

Esempio

I tre esempi seguenti illustrano una pagina Web, una classe di pagine code-behind e una classe di accesso ai dati che consentono all'utente di selezionare il numero di record visualizzati nella pagina.

La pagina Web contiene un ObjectDataSource controllo la cui EnablePaging proprietà è impostata su true. La SelectCountMethod proprietà è impostata sul nome di un metodo che restituisce il numero totale di record nella query. La MaximumRowsParameterName proprietà e la StartRowIndexParameterName proprietà vengono impostate sui nomi dei parametri utilizzati nel metodo Select. La pagina contiene anche un DropDownList controllo .

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

Il secondo esempio mostra un gestore per l'evento ListControl.SelectedIndexChanged del DropDownList controllo . Il codice nel gestore imposta la PageSize proprietà sulla selezione dell'utente.

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

Il terzo esempio mostra la classe di accesso ai dati che recupera i dati dalla tabella Customers. Include un metodo denominato GetSubsetOfEmployees, assegnato alla SelectMethod proprietà del ObjectDataSource controllo . Nell'esempio è incluso anche un metodo denominato GetEmployeeCount, assegnato alla SelectCountMethod proprietà del ObjectDataSource controllo . La classe usa LINQ per eseguire una query sulla tabella Customers. L'esempio richiede una classe LINQ to SQL che rappresenta il database Northwind e la tabella Customers. Per altre informazioni, vedere Procedura: Creare classi LINQ to SQL in un progetto 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

Commenti

Il paging da parte del ObjectDataSource controllo viene gestito impostando le EnablePagingproprietà , StartRowIndexParameterName, MaximumRowsParameterNamee SelectCountMethod di ObjectDataSource e definendo un metodo select nell'oggetto business con i parametri appropriati. Quando la EnablePaging proprietà è impostata su true, l'insieme SelectParameters include due parametri aggiuntivi per la prima riga richiesta e il numero di righe richieste. Questi due parametri sono denominati come definiti dalle StartRowIndexParameterName proprietà e MaximumRowsParameterName . Il Select metodo deve restituire il numero richiesto di righe, a partire dall'indice specificato. Poiché i dati potrebbero non essere divisi in modo uniforme per le dimensioni della pagina, l'ultima pagina potrebbe contenere meno righe. Di conseguenza, il numero di righe richieste è in realtà il numero massimo di righe restituite.

Quando il paging è abilitato nel controllo associato a dati, il controllo associato a dati chiama il metodo con l'indice iniziale e il Select numero di righe necessarie. Inoltre, se la SelectCountMethod proprietà è impostata, il controllo associato a dati chiama il metodo prima di eseguire il rendering dei controlli pager. Ad esempio, se il paging di un GridView controllo è abilitato con dimensioni di pagina pari a 5 e il metodo specificato dalla SelectCountMethod proprietà restituisce 20, nel cercapersone vengono visualizzate solo 4 pagine.

La EnablePaging proprietà delega alla EnablePaging proprietà dell'oggetto ObjectDataSourceView .

Si applica a

Vedi anche