LinqDataSourceStatusEventArgs.TotalRowCount 屬性

定義

取得資料擷取作業中資料集的資料列總數。

public:
 property int TotalRowCount { int get(); };
public int TotalRowCount { get; }
member this.TotalRowCount : int
Public ReadOnly Property TotalRowCount As Integer

屬性值

資料擷取作業中資料集的資料列總數。如果資料修改作業期間建立了 LinqDataSourceStatusEventArgs 物件,則為 -1;如果您將 AutoPage 設為 true 並且將 RetrieveTotalRowCount 設為 false,藉此啟用自訂的分頁,則為 -1。

範例

下列範例顯示具有 LinqDataSource 控制項、 GridView 控制項和 Literal 控制項的網頁。 控制項 LinqDataSource 會定義 事件的事件處理常式 Selected

<asp:Literal ID="Literal1" runat="server"></asp:Literal> Total Records
<br />
<asp:LinqDataSource 
  AutoPage="true"
  ID="LinqDataSource1" 
  runat="server" 
  ContextTypeName="ExampleDataContext" 
  TableName="Customers" 
  onselected="LinqDataSource1_Selected">
</asp:LinqDataSource>
<asp:GridView 
  ID="GridView1" 
  runat="server" 
  AllowPaging="true" 
  AutoGenerateColumns="True" 
  DataKeyNames="CustomerID" 
  DataSourceID="LinqDataSource1">
</asp:GridView>
<asp:Literal ID="Literal1" runat="server"></asp:Literal> Total Records
<br />
<asp:LinqDataSource 
  AutoPage="true"
  ID="LinqDataSource1" 
  runat="server" 
  ContextTypeName="ExampleDataContext" 
  TableName="Customers">
</asp:LinqDataSource>
<asp:GridView 
  ID="GridView1" 
  runat="server" 
  AllowPaging="true" 
  AutoGenerateColumns="True" 
  DataKeyNames="CustomerID" 
  DataSourceID="LinqDataSource1">
</asp:GridView>

下列範例顯示 事件的事件處理常式 Selected 程式碼。 屬性的值 TotalRowCount 會指派給 Literal 控制項。

protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    Literal1.Text = e.TotalRowCount.ToString();
}
Protected Sub LinqDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Selected
    Literal1.Text = e.TotalRowCount.ToString()
End Sub

備註

您可以使用 TotalRowCount 屬性在資料擷取作業期間取得資料集中的記錄數目。 一般而言,當您顯示資料頁面,並想要顯示記錄總數時,您會擷取此屬性。

正在分頁資料時,屬性中的 TotalRowCount 值可能與查詢所傳回的實際資料列數目不同。 這是因為查詢只會傳回該資料頁所需的資料列數目。

TotalRowCount只有當查詢在事件的事件處理常式中存取時,屬性才會包含來自查詢的 Selected 記錄數目。 從 、 DeletedInserted 、 或 Updated 事件的事件處理常式 ContextCreated 存取屬性時, TotalRowCount 屬性會包含 -1。

屬性的值 TotalRowCount 取決於控制項的 LinqDataSource 屬性,以及 AllowPaging 資料繫結 AutoPage 控制項的 屬性。 下表摘要說明 屬性的 TotalRowCount 可能值。

LinqDataSource 控制項的 AutoPage 屬性 資料繫結控制項的 AllowPaging 屬性 TotalRowCount 屬性 結果
true true 查詢的記錄數目。 資料會自動分頁。
false false 查詢的記錄數目。 未分頁資料。
true false -1 未分頁資料。
false true 您在事件的事件處理常式中指派給 TotalRowCount 屬性的值 Selecting 資料會根據您在自訂分頁時指定的值進行分頁。

適用於