ObjectDataSource.SelectCountMethod 속성

정의

ObjectDataSource 컨트롤이 행 수를 검색할 때 호출하는 메서드나 함수의 이름을 가져오거나 설정합니다.

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가 행 수를 검색할 때 사용하는 메서드나 함수의 이름을 나타내는 문자열입니다. 이 메서드는 정수(Int32)를 반환해야 합니다. 기본값은 빈 문자열("")입니다.

예제

다음 세 가지 예제에는 웹 페이지, 코드 숨김 페이지 클래스 및 사용자는 페이지에 표시 된 레코드 수를 선택할 수 있도록 데이터 액세스 클래스를 보여 줍니다.

웹 페이지에는 ObjectDataSource 컨트롤 EnablePaging 속성이 true합니다. SelectCountMethod 쿼리에서 레코드의 총 수를 반환 하는 메서드의 이름으로 설정 합니다. 합니다 MaximumRowsParameterName 속성 및 StartRowIndexParameterName 속성 선택 메서드에서 사용 되는 매개 변수의 이름으로 설정 됩니다. 페이지는 DropDownList 제어 합니다.

<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 제어 합니다. 처리기 집합의 코드는 PageSize 속성을 사용자의 선택 합니다.

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 테이블에서 데이터를 검색 하는 데이터 액세스 클래스를 보여 줍니다. 라는 메서드를 포함 GetSubsetOfEmployees에 할당 되는 SelectMethod 의 속성을 ObjectDataSource 컨트롤입니다. 예제에는 또한 라는 메서드 GetEmployeeCount에 할당 되는 SelectCountMethod 의 속성은 ObjectDataSource 컨트롤입니다. LINQ를 사용 하 여 Customers 테이블을 쿼리 하는 클래스입니다. 이 예제에서는 LINQ to SQL 클래스 Northwind 데이터베이스와 Customers 테이블을 나타내는 필요 합니다. 자세한 내용은 방법: 만들 LINQ to SQL 클래스를 웹 프로젝트에서합니다.

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 속성은 데이터 소스 페이징을 지원 하기 위해 총 행 수를 검색 하는 데 사용 되는 비즈니스 개체 메서드를 식별 합니다. SelectCountMethod 속성은 경우에 평가 합니다 EnablePaging 속성이 true.

SelectCountMethod 에 위임 하는 속성을 SelectCountMethod 의 속성을 ObjectDataSourceView 개체와 연결 된는 ObjectDataSource 컨트롤. 페이징을 지 원하는 방법에 대 한 자세한 합니다 ObjectDataSource 제어를 참조 하십시오 EnablePaging합니다.

적용 대상

추가 정보