GridView.AllowPaging 属性

定义

获取或设置一个值,该值指示是否启用分页功能。Gets or sets a value indicating whether the paging feature is enabled.

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

属性值

如果启用分页功能,则为 true;否则为 falsetrue if the paging feature is enabled; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示如何使用 AllowPaging 属性以声明方式启用 GridView 控件中的分页功能。The following example demonstrates how to use the AllowPaging property to declaratively enable the paging feature in the GridView control.


<%@ 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 控件可以自动将记录分为多页,而不是同时在数据源中显示所有记录。Instead of displaying all the records in the data source at the same time, the GridView control can automatically break the records up into pages. 如果数据源支持分页功能,则 GridView 控件可以利用该功能,并提供内置的分页功能。If the data source supports the paging capability, the GridView control can take advantage of that and provide built-in paging functionality. 分页功能可用于支持 System.Collections.ICollection 接口的任何数据源对象或支持分页功能的数据源。The paging feature can be used with any data source object that supports the System.Collections.ICollection interface or a data source that supports paging capability.

若要启用分页功能,请将 AllowPaging 属性设置为 trueTo enable the paging feature, set the AllowPaging property to true. 默认情况下,GridView 控件一次一页显示10条记录。By default, the GridView control displays 10 records on a page at a time. 您可以通过设置 "PageSize" 属性来更改页面上显示的记录数。You can change the number of records displayed on a page by setting the PageSize property. 若要确定显示数据源内容所需的总页数,请使用 PageCount 属性。To determine the total number of pages required to display the data source contents, use the PageCount property. 您可以通过使用 PageIndex 属性来确定当前显示的页的索引。You can determine the index of the currently displayed page by using the PageIndex property.

启用分页时,会自动在 GridView 控件中显示一个名为 "页导航行" 的附加行。When paging is enabled, an additional row called the pager row is automatically displayed in the GridView control. 页导航行包含允许用户导航到其他页的控件。The pager row contains controls that allow the user to navigate to the other pages. 通过使用 "PagerSettings" 属性,可以控制页导航行的设置(如页导航显示模式、一次显示的页链接的数目以及页导航控件的文本标签)。You can control the settings of the pager row (such as the pager display mode, the number of page links to display at a time, and the pager control's text labels) by using the PagerSettings property. 通过设置 "Position" 属性,可以在控件的顶部、底部或同时显示页的顶部和底部。The pager row can be displayed at the top, bottom, or both the top and bottom of the control by setting the Position property. 还可以通过设置 "Mode" 属性,从四个内置的页导航显示模式中选择一个。You can also select from one of four built-in pager display modes by setting the Mode property. 下表介绍了内置的显示模式。The following table describes the built-in display modes.

模式Mode 说明Description
PagerButton.NextPrevious 一组由 "上一页" 和 "下一页" 按钮组成的分页控件。A set of pagination controls consisting of previous and next buttons.
PagerButton.NextPreviousFirstLast 由 "上一个"、"下一步"、"上一步" 和 "最后一个" 按钮组成A set of pagination controls consisting of previous, next, first, and last buttons.
PagerButton.Numeric 一组由用于直接访问页的带编号的链接按钮组成的分页控件。A set of pagination controls consisting of numbered link buttons to access pages directly. 这是默认模式。This is the default mode.
PagerButton.NumericFirstLast 一组由编号和第一个和最后一个链接按钮组成的分页控件。A set of pagination controls consisting of numbered and first and last link buttons.

若要控制页导航行的外观(包括其背景色、字体颜色和位置),请使用 "PagerStyle" 属性。To control the appearance of the pager row (including its background color, font color, and position), use the PagerStyle property.

备注

当数据源仅包含一页记录时,GridView 控件将自动隐藏页导航行。The GridView control automatically hides the pager row when the data source contains only one page of records.

GridView 控件还允许您为页导航行定义自定义模板。The GridView control also enables you to define a custom template for the pager row. 有关创建自定义页导航行模板的详细信息,请参阅 PagerTemplateFor more information about creating a custom pager row template, see PagerTemplate.

GridView 控件提供了多个可用于在进行分页时执行自定义操作的事件。The GridView control provides several events that you can use to perform a custom action when paging occurs. 下表列出了可用的事件。The following table lists the available events.

ㄆンEvent 说明Description
PageIndexChanged 在单击某一页导航按钮时,但在 GridView 控件处理分页操作之后发生。Occurs when one of the pager buttons is clicked, but after the GridView control handles the paging operation. 当用户导航到控件中的其他页面后,如果需要执行某个任务,通常会使用此事件。This event is commonly used when you need to perform a task after the user navigates to a different page in the control.
PageIndexChanging 在单击某一页导航按钮时,但在 GridView 控件处理分页操作之前发生。Occurs when one of the pager buttons is clicked, but before the GridView control handles the paging operation. 此事件通常用于取消分页操作。This event is often used to cancel the paging operation.

适用于

另请参阅