ObjectDataSource.StartRowIndexParameterName Proprietà

Definizione

Ottiene o imposta il nome di parametro del metodo di recupero dati utilizzato per indicare il valore dell'identificatore del primo record da recuperare per il supporto paging dell'origine dati.

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

Valore della proprietà

Nome di parametro del metodo dell'oggetto business utilizzato per indicare il primo record da recuperare. Il parametro deve restituire un Integer. Il valore predefinito è "startRowIndex".

Esempio

I tre esempi seguenti illustrano una pagina Web, una classe di pagine code-behind e una classe di accesso ai dati che consentono all'utente di selezionare il numero di record visualizzati nella pagina.

La pagina Web contiene un ObjectDataSource controllo la cui EnablePaging proprietà è impostata su true. La SelectCountMethod proprietà è impostata sul nome di un metodo che restituisce il numero totale di record nella query. La MaximumRowsParameterName proprietà e la StartRowIndexParameterName proprietà vengono impostate sui nomi dei parametri utilizzati nel metodo Select. La pagina contiene anche un DropDownList controllo .

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

Il secondo esempio mostra un gestore per l'evento ListControl.SelectedIndexChanged del DropDownList controllo . Il codice nel gestore imposta la PageSize proprietà sulla selezione dell'utente.

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

Il terzo esempio mostra la classe di accesso ai dati che recupera i dati dalla tabella Customers. Include un metodo denominato GetSubsetOfEmployees, assegnato alla SelectMethod proprietà del ObjectDataSource controllo . Nell'esempio è incluso anche un metodo denominato GetEmployeeCount, assegnato alla SelectCountMethod proprietà del ObjectDataSource controllo . La classe usa LINQ per eseguire una query sulla tabella Customers. L'esempio richiede una classe LINQ to SQL che rappresenta il database Northwind e la tabella Customers. Per altre informazioni, vedere Procedura: Creare classi LINQ to SQL in un progetto Web.

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

Commenti

La StartRowIndexParameterName proprietà viene utilizzata per supportare il paging dell'origine dati. Per informazioni su come il ObjectDataSource paging è supportato dal controllo , vedere EnablePaging.

La StartRowIndexParameterName proprietà delega alla StartRowIndexParameterName proprietà dell'oggetto ObjectDataSourceView associato al ObjectDataSource controllo .

Si applica a

Vedi anche