ObjectDataSource.SelectCountMethod Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the name of the method or function that the ObjectDataSource control invokes to retrieve a row count.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

Public Property SelectCountMethod As String

Property Value

Type: System.String

A string that represents the name of the method or function that the ObjectDataSource uses to retrieve a row count. The method must return an integer (Int32). The default is an empty string ("").

The SelectCountMethod property identifies a business object method that is used to retrieve a total row count, to support data source paging. The SelectCountMethod property is evaluated only if the EnablePaging property is set to true.

The SelectCountMethod property delegates to the SelectCountMethod property of the ObjectDataSourceView object that is associated with the ObjectDataSource control. For information about how paging is supported by the ObjectDataSource control, see EnablePaging.

The following three examples show a Web page, a code-behind page class, and a data-access class that enable the user to pick how many records are displayed in the page.

The Web page contains an ObjectDataSource control whose EnablePaging property is set to true. The SelectCountMethod property is set to the name of a method that returns the total number of records in the query. The MaximumRowsParameterName property and the StartRowIndexParameterName property are set to the names of the parameters that are used in the Select method. The page also contains a DropDownList control.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    How many rows to display on this page:<br />
        <asp:ListItem Value="5"></asp:ListItem>
        <asp:ListItem Value="10" Selected="True"></asp:ListItem>
        <asp:ListItem Value="20"></asp:ListItem>




The second example shows a handler for the ListControl.SelectedIndexChanged event of the DropDownList control. The code in the handler sets the PageSize property to the user's selection.

Protected Sub rowsToDisplay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rowsToDisplay.SelectedIndexChanged
    GridView1.PageSize = Integer.Parse(rowsToDisplay.SelectedValue)
End Sub

The third example shows the data access class that retrieves data from the Customers table. It includes a method named GetSubsetOfEmployees, which is assigned to the SelectMethod property of the ObjectDataSource control. The example also includes a method named GetEmployeeCount, which is assigned to the SelectCountMethod property of the ObjectDataSource control. The class uses LINQ to query the Customers table. The example requires a LINQ to SQL class that represents the Northwind database and Customers table. For more information, see How to: Create LINQ to SQL Classes in a Web Project.

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

.NET Framework
Available since 2.0
Return to top