ObjectDataSource.StartRowIndexParameterName 屬性

定義

取得或設定資料擷取方法參數的名稱,用於指出為資料來源分頁支援所擷取之第一個資料錄的識別項的值。

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

屬性值

用於指出要擷取之第一個資料錄的商務物件方法參數名稱。 參數必須傳回整數值。 預設值為 "startRowIndex"

範例

下列三個範例顯示網頁、程式碼後置頁面類別,以及可讓使用者挑選頁面上顯示多少筆記錄的資料存取類別。

網頁包含 ObjectDataSource 控制項,其 EnablePaging 屬性設定為 true 。 屬性 SelectCountMethod 會設定為傳回查詢中記錄總數的方法名稱。 屬性 MaximumRowsParameterNameStartRowIndexParameterName 屬性會設定為 Select 方法中使用的參數名稱。 頁面也包含 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>

第二個範例顯示 控制項事件的 DropDownList 處理常式 ListControl.SelectedIndexChanged 。 處理常式中的程式碼會將 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 資料表。 此範例需要代表 Northwind 資料庫和 Customers 資料表的 LINQ to SQL 類別。 如需詳細資訊,請參閱如何:在 Web 專案中建立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

備註

屬性 StartRowIndexParameterName 是用來支援資料來源分頁。 如需 控制項如何支援 ObjectDataSource 分頁的資訊,請參閱 EnablePaging

屬性 StartRowIndexParameterName 會委派給 StartRowIndexParameterNameObjectDataSource 控制項相關聯之 ObjectDataSourceView 物件的 屬性。

適用於

另請參閱