ObjectDataSource.SelectCountMethod Propiedad

Definición

Obtiene o establece el nombre del método o función a los que invoca el control ObjectDataSource para recuperar un recuento de filas.

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

Valor de propiedad

Cadena que representa el nombre del método o función que usa el control ObjectDataSource para recuperar un recuento de filas. El método debe devolver un entero (Int32). El valor predeterminado es una cadena vacía ("").

Ejemplos

En los tres ejemplos siguientes se muestra una página web, una clase de página subyacente y una clase de acceso a datos que permite al usuario elegir cuántos registros se muestran en la página.

La página web contiene un ObjectDataSource control cuya EnablePaging propiedad se establece trueen . La SelectCountMethod propiedad se establece en el nombre de un método que devuelve el número total de registros de la consulta. La MaximumRowsParameterName propiedad y la StartRowIndexParameterName propiedad se establecen en los nombres de los parámetros que se usan en el método Select. La página también contiene un DropDownList control .

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

En el segundo ejemplo se muestra un controlador para el ListControl.SelectedIndexChanged evento del DropDownList control . El código del controlador establece la PageSize propiedad en la selección del usuario.

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

En el tercer ejemplo se muestra la clase de acceso a datos que recupera datos de la tabla Customers. Incluye un método denominado GetSubsetOfEmployees, que se asigna a la SelectMethod propiedad del ObjectDataSource control . En el ejemplo también se incluye un método denominado GetEmployeeCount, que se asigna a la SelectCountMethod propiedad del ObjectDataSource control . La clase usa LINQ para consultar la tabla Customers. El ejemplo requiere una clase LINQ to SQL que representa la base de datos Northwind y la tabla Customers. Para obtener más información, vea How to: Create LINQ to SQL Classes in a Web Project.

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

Comentarios

La SelectCountMethod propiedad identifica un método de objeto de negocio que se usa para recuperar un recuento total de filas, para admitir la paginación del origen de datos. La SelectCountMethod propiedad solo se evalúa si la EnablePaging propiedad está establecida trueen .

La SelectCountMethod propiedad delega a la SelectCountMethod propiedad del ObjectDataSourceView objeto asociado al ObjectDataSource control . Para obtener información sobre cómo el control admite la ObjectDataSource paginación, vea EnablePaging.

Se aplica a

Consulte también