ObjectDataSource.SelectCountMethod 属性

定义

获取或设置 ObjectDataSource 控件调用以检索行数的方法或函数的名称。Gets or sets the name of the method or function that the ObjectDataSource control invokes to retrieve a row count.

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

属性值

表示 ObjectDataSource 用于检索行数的方法或函数的名称的字符串。A string that represents the name of the method or function that the ObjectDataSource uses to retrieve a row count. 此方法必须返回一个整数 (Int32)。The method must return an integer (Int32). 默认值为空字符串("")。The default is an empty string ("").

示例

以下三个示例演示了一个网页、一个代码隐藏页类和一个数据访问类, 使用户能够选择页面中显示的记录数。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.

网页包含一个ObjectDataSource控件, 其EnablePaging属性设置为trueThe Web page contains an ObjectDataSource control whose EnablePaging property is set to true. SelectCountMethod属性设置为方法的名称, 该方法返回查询中的记录总数。The SelectCountMethod property is set to the name of a method that returns the total number of records in the query. MaximumRowsParameterName 属性StartRowIndexParameterName和属性设置为 Select 方法中使用的参数的名称。The MaximumRowsParameterName property and the StartRowIndexParameterName property are set to the names of the parameters that are used in the Select method. 此页还包含一个DropDownList控件。The page also contains a 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>

第二个示例演示ListControl.SelectedIndexChanged DropDownList控件的事件的处理程序。The second example shows a handler for the ListControl.SelectedIndexChanged event of the DropDownList control. 处理程序中的代码将PageSize属性设置为用户的选择。The code in the handler sets the PageSize property to the user's selection.

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

第三个示例显示了从 Customers 表检索数据的数据访问类。The third example shows the data access class that retrieves data from the Customers table. 它包括一个名GetSubsetOfEmployees为的方法, 该方法分配SelectMethodObjectDataSource控件的属性。It includes a method named GetSubsetOfEmployees, which is assigned to the SelectMethod property of the ObjectDataSource control. 该示例还包括一个名GetEmployeeCount为的方法, 该方法分配SelectCountMethodObjectDataSource控件的属性。The example also includes a method named GetEmployeeCount, which is assigned to the SelectCountMethod property of the ObjectDataSource control. 类使用 LINQ 查询 Customers 表。The class uses LINQ to query the Customers table. 该示例需要一个表示 Northwind 数据库和 Customers 表的 LINQ to SQL 类。The example requires a LINQ to SQL class that represents the Northwind database and Customers table. 有关详细信息,请参阅如何:在 Web 项目中创建 LINQ to SQL 类。For more information, see 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

注解

SelectCountMethod属性标识用于检索总行数的业务对象方法, 以支持数据源分页。The SelectCountMethod property identifies a business object method that is used to retrieve a total row count, to support data source paging. 仅当属性设置为true时, 才计算属性。SelectCountMethod EnablePagingThe SelectCountMethod property is evaluated only if the EnablePaging property is set to true.

属性委托ObjectDataSourceView SelectCountMethod给与ObjectDataSource控件相关联的对象的属性。 SelectCountMethodThe SelectCountMethod property delegates to the SelectCountMethod property of the ObjectDataSourceView object that is associated with the ObjectDataSource control. 有关ObjectDataSource控件如何支持分页的信息, 请参阅EnablePagingFor information about how paging is supported by the ObjectDataSource control, see EnablePaging.

适用于

另请参阅