GridView.AllowPaging 屬性

定義

取得或設定值,指出是否啟用分頁功能。

public:
 virtual property bool AllowPaging { bool get(); void set(bool value); };
public virtual bool AllowPaging { get; set; }
member this.AllowPaging : bool with get, set
Public Overridable Property AllowPaging As Boolean

屬性值

Boolean

如果啟用分頁功能則為 true,否則為 false。 預設為 false

範例

下列範例示範如何使用 AllowPaging 屬性,在 控制項中 GridView 以宣告方式啟用分頁功能。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView AllowPaging Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView AllowPaging Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">
                
        <pagersettings mode="Numeric"
          position="Bottom"           
          pagebuttoncount="10"/>
                      
        <pagerstyle backcolor="LightBlue"
          height="30px"
          verticalalign="Bottom"
          horizontalalign="Center"/>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView AllowPaging Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView AllowPaging Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">
                
        <pagersettings mode="Numeric"
          position="Bottom"           
          pagebuttoncount="10"/>
                      
        <pagerstyle backcolor="LightBlue"
          height="30px"
          verticalalign="Bottom"
          horizontalalign="Center"/>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

備註

控制項可以自動將記錄分成分頁, GridView 而不是同時顯示資料來源中的所有記錄。 如果資料來源支援分頁功能,控制項 GridView 就可以利用該功能,並提供內建分頁功能。 分頁功能可以搭配任何支援 System.Collections.ICollection 介面的資料來源物件或支援分頁功能的資料來源使用。

若要啟用分頁功能,請將 AllowPaging 屬性設定為 true 。 根據預設, GridView 控制項一次會在頁面上顯示 10 筆記錄。 您可以藉由設定 PageSize 屬性來變更頁面上顯示的記錄數目。 若要判斷顯示資料來源內容所需的總頁數,請使用 PageCount 屬性。 您可以使用 屬性來判斷目前顯示頁面 PageIndex 的索引。

啟用分頁時,會在 控制項中 GridView 自動顯示稱為呼叫器資料列的額外資料列。 呼叫器資料列包含控制項,可讓使用者流覽至其他頁面。 您可以使用 屬性來控制頁面巡覽列 (的設定,例如呼叫器顯示模式、一次顯示的頁面連結數目,以及呼叫器控制項的文字標籤) PagerSettings 。 呼叫器資料列可以透過設定 Position 屬性,顯示在控制項的頂端、底端或兩者。 您也可以藉由設定 Mode 屬性,從四個內建的呼叫器顯示模式之一中選取。 下表描述內建的顯示模式。

[模式] 描述
PagerButton.NextPrevious 一組由上一個和下一個按鈕組成的分頁控制項。
PagerButton.NextPreviousFirstLast 一組分頁控制項,包含上一個、下一個、第一個和最後一個按鈕。
PagerButton.Numeric 一組分頁控制項,包含用於直接存取頁面的編號連結按鈕。 這是預設模式。
PagerButton.NumericFirstLast 一組分頁控制項,其中包含編號和第一個和最後一個連結按鈕。

若要控制頁面巡覽列的外觀, (包括其背景色彩、字型色彩和位置) ,請使用 PagerStyle 屬性。

注意

當資料來源只包含一頁記錄時,控制項 GridView 會自動隱藏呼叫器資料列。

控制項 GridView 也可讓您定義呼叫器資料列的自訂範本。 如需建立自訂呼叫器資料列範本的詳細資訊,請參閱 PagerTemplate

控制項 GridView 提供數個事件,可讓您在分頁發生時用來執行自訂動作。 下表列出可用的事件。

事件 描述
PageIndexChanged 按一下其中一個頁面巡覽區按鈕時發生 (但在 GridView 控制項處理分頁作業之後)。 當您需要在使用者流覽至控制項中的不同頁面之後執行工作時,通常會使用此事件。
PageIndexChanging 發生於按一下其中一個頁面巡覽區按鈕時,但是在 GridView 控制項處理分頁作業之前。 此事件通常用來取消分頁作業。

適用於

另請參閱