ObjectDataSource.SelectCountMethod Propriété

Définition

Obtient ou définit le nom de la méthode ou de la fonction que le contrôle ObjectDataSource appelle pour récupérer un nombre de lignes.

public:
 property System::String ^ SelectCountMethod { System::String ^ get(); void set(System::String ^ value); };
public string SelectCountMethod { get; set; }
member this.SelectCountMethod : string with get, set
Public Property SelectCountMethod As String

Valeur de propriété

Une chaîne qui représente le nom de la méthode ou de la fonction utilisé par ObjectDataSource pour récupérer un nombre de lignes. La méthode doit retourner un entier (Int32). La valeur par défaut est une chaîne vide ("").

Exemples

Les trois exemples suivants montrent une page Web, une classe de page code-behind et une classe d’accès aux données qui permettent à l’utilisateur de choisir le nombre d’enregistrements affichés dans la page.

La page Web contient un ObjectDataSource contrôle dont EnablePaging la propriété est définie sur true. La SelectCountMethod propriété est définie sur le nom d’une méthode qui retourne le nombre total d’enregistrements dans la requête. La MaximumRowsParameterName propriété et la StartRowIndexParameterName propriété sont définies sur les noms des paramètres utilisés dans la méthode Select. La page contient également un DropDownList contrôle.

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

Le deuxième exemple montre un gestionnaire pour l’événement ListControl.SelectedIndexChanged du DropDownList contrôle. Le code dans le gestionnaire définit la PageSize propriété sur la sélection de l’utilisateur.

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

Le troisième exemple montre la classe d’accès aux données qui récupère les données de la table Customers. Il inclut une méthode nommée GetSubsetOfEmployees, qui est affectée à la SelectMethod propriété du ObjectDataSource contrôle. L’exemple inclut également une méthode nommée GetEmployeeCount, qui est affectée à la SelectCountMethod propriété du ObjectDataSource contrôle. La classe utilise LINQ pour interroger la table Customers. L’exemple nécessite une classe LINQ to SQL qui représente la base de données Northwind et la table Customers. Pour plus d’informations, consultez Guide pratique pour créer des classes LINQ to SQL dans un projet 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

Remarques

La SelectCountMethod propriété identifie une méthode d’objet métier utilisée pour récupérer un nombre total de lignes, afin de prendre en charge la pagination de source de données. La SelectCountMethod propriété est évaluée uniquement si la propriété a la EnablePaging valeur true.

La SelectCountMethod propriété délègue à la SelectCountMethod propriété de l’objet ObjectDataSourceView associé au ObjectDataSource contrôle. Pour plus d’informations sur la prise en charge de la pagination par le ObjectDataSource contrôle, consultez EnablePaging.

S’applique à

Voir aussi