Freigeben über


DataGrid.AllowCustomPaging-Eigenschaft

Ruft einen Wert ab, der angibt, ob die benutzerdefinierte Paginierung aktiviert ist, oder legt diesen fest.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Public Overridable Property AllowCustomPaging As Boolean
'Usage
Dim instance As DataGrid
Dim value As Boolean

value = instance.AllowCustomPaging

instance.AllowCustomPaging = value
public virtual bool AllowCustomPaging { get; set; }
public:
virtual property bool AllowCustomPaging {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_AllowCustomPaging ()

/** @property */
public void set_AllowCustomPaging (boolean value)
public function get AllowCustomPaging () : boolean

public function set AllowCustomPaging (value : boolean)

Eigenschaftenwert

true, wenn benutzerdefiniertes Paging aktiviert ist, andernfalls false. Der Standardwert ist false.

Hinweise

Das Paging ermöglicht Ihnen das Anzeigen des Inhalts des DataGrid-Steuerelements in Seitensegmenten. Die Anzahl der Elemente auf einer Seite wird durch die PageSize-Eigenschaft bestimmt. Wenn kein Wert für die PageSize-Eigenschaft angegeben ist, zeigt DataGrid zehn Elemente auf einer Seite an.

In der Regel wird jedes Mal, wenn das DataGrid-Steuerelement zu einer anderen Seite wechselt, eine Datenquelle mit allen Zeilen im DataGrid-Steuerelement geladen. Bei einer sehr großen Datenquelle kann dadurch eine große Menge von Ressourcen gebunden werden. Die benutzerdefinierte Paginierung ermöglicht es Ihnen, nur das Datensegment zu laden, das für die Anzeige einer einzigen Seite benötigt wird.

Um das benutzerdefinierte Paging zu aktivieren, legen Sie sowohl die AllowPaging-Eigenschaft als auch die AllowCustomPaging-Eigenschaft auf true fest. Geben Sie anschließend Code zum Behandeln des PageIndexChanged-Ereignisses an.

Die typische Logik für den PageIndexChanged-Ereignishandler besteht darin, zuerst die CurrentPageIndex-Eigenschaft auf den Index der anzuzeigenden Seite festzulegen.

Hinweis

Der Ereignishandler empfängt ein DataGridPageChangedEventArgs-Objekt als Parameter. Sie können mit der NewPageIndex-Eigenschaft dieses Parameters den Index der Seite bestimmen, die vom Benutzer in den Seitenauswahlelementen des DataGrid-Steuerelements ausgewählt wurde.

Erstellen Sie anschließend eine Datenquelle mit den Daten, die auf einer einzigen Seite angezeigt werden sollen, und binden Sie dann die Daten mit der DataBind-Methode an das DataGrid-Steuerelement.

Hinweis

Da nur ein Segment der Daten geladen ist, müssen Sie die VirtualItemCount-Eigenschaft auf die Gesamtzahl der Elemente im DataGrid-Steuerelement festlegen. Dadurch kann das Steuerelement die Gesamtzahl der für die Anzeige aller Elemente im DataGrid-Steuerelement benötigten Seiten bestimmen. Diese Eigenschaft wird normalerweise programmgesteuert festgelegt, nachdem die Gesamtzahl der Elemente im DataGrid-Steuerelement bestimmt wurde.

Wenn das Paging aktiviert und die AllowCustomPaging-Eigenschaft auf false festgelegt ist, geht das DataGrid-Steuerelement davon aus, dass die Datenquelle alle anzuzeigenden Elemente enthält. Das DataGrid-Steuerelement berechnet die Indizes der Elemente auf der angezeigten Seite anhand des Seitenindex, der durch die CurrentPageIndex-Eigenschaft angegeben wird, sowie anhand der Anzahl der Elemente auf einer Seite, die durch die PageSize-Eigenschaft angegeben wird.

Wenn die AllowCustomPaging-Eigenschaft auf true festgelegt ist, geht das DataGrid-Steuerelement davon aus, dass die Datenquelle nur die Elemente enthält, die von der VirtualItemCount-Eigenschaft bestimmt wurden. Alle Elemente bis zur von der PageSize-Eigenschaft angegebenen Anzahl von Elementen werden angezeigt.

Beispiel

Das folgende Codebeispiel veranschaulicht die Aktivierung des benutzerdefinierten Pagings mithilfe der AllowCustomPaging-Eigenschaft.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>

   <script runat="server">

      ' Normally, an entire data source is loaded in the DataGrid control, 
      ' which can take up a lot of resources. This example uses custom 
      ' paging, which loads only the segment of data needed to fill a
      ' single page. In order to query for the appropriate segment of
      ' data, the index of the first item displayed on a page must be
      ' tracked as the user navigates between pages.
      Dim startIndex As Integer = 0

      Function CreateDataSource() As ICollection 

         ' Create sample data for the DataGrid control.
         Dim dt As DataTable = New DataTable()
         Dim dr As DataRow

         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
         dt.Columns.Add(New DataColumn("DateTimeValue", GetType(String)))
         dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))

         ' Populate the table with sample values. When using custom paging,
         ' a query should only return enough data to fill a single page, 
         ' beginning at the start index.
         Dim i As Integer         

         For i = startIndex To ((startIndex + MyDataGrid.PageSize) - 1) 

             dr = dt.NewRow()

             dr(0) = i
             dr(1) = "Item " & i.ToString()
             dr(2) = DateTime.Now.ToShortDateString()
             If (i Mod 2 <> 0) Then
                dr(3) = True
             Else
                dr(3) = False
             End If

             dt.Rows.Add(dr)

         Next i

         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function

      Sub Page_Load(sender As Object, e As EventArgs) 

         ' Load sample data only once, when the page is first loaded.
         If Not IsPostBack Then 

            ' Set the virtual item count, which specifies the total number
            ' items displayed in the DataGrid control when custom paging
            ' is used.
            MyDataGrid.VirtualItemCount = 200

            ' Retrieve the segment of data to display on the page from the 
            ' data source and bind it to the DataGrid control.
            BindGrid()

         End If

      End Sub

      Sub MyDataGrid_Page(sender as Object, e As DataGridPageChangedEventArgs) 

         ' For the DataGrid control to navigate to the correct page when
         ' paging is allowed, the CurrentPageIndex property must be updated
         ' programmatically. This process is usually accomplished in the
         ' event-handling method for the PageIndexChanged event.

         ' Set CurrentPageIndex to the page the user clicked.
         MyDataGrid.CurrentPageIndex = e.NewPageIndex

         ' Calculate the index of the first item to display on the page 
         ' using the current page index and the page size.
         startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize

         ' Retrieve the segment of data to display on the page from the 
         ' data source and bind it to the DataGrid control.
         BindGrid()

      End Sub

      Sub BindGrid() 

         MyDataGrid.DataSource = CreateDataSource()
         MyDataGrid.DataBind()

      End Sub

   </script>

<body>

   <form runat="server">
 
      <h3> DataGrid Custom Paging Example </h3>

      <asp:DataGrid id="MyDataGrid" 
           AllowCustomPaging="True" 
           AllowPaging="True" 
           PageSize="10" 
           OnPageIndexChanged="MyDataGrid_Page" 
           runat="server">

         <HeaderStyle BackColor="Navy" 
                      ForeColor="White" 
                      Font-Bold="True" />

         <PagerStyle Mode="NumericPages" 
                     HorizontalAlign="Right" />

      </asp:DataGrid>

   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<html>

   <script runat="server">

      // Normally, an entire data source is loaded in the DataGrid control, 
      // which can take up a lot of resources. This example uses custom 
      // paging, which loads only the segment of data needed to fill a
      // single page. In order to query for the appropriate segment of
      // data, the index of the first item displayed on a page must be
      // tracked as the user navigates between pages.
      int startIndex = 0;

      ICollection CreateDataSource() 
      {

         // Create sample data for the DataGrid control.
         DataTable dt = new DataTable();
         DataRow dr;

         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("DateTimeValue", typeof(string)));
         dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));

         // Populate the table with sample values. When using custom paging,
         // a query should only return enough data to fill a single page, 
         // beginning at the start index.
         for (int i = startIndex; i < (startIndex + MyDataGrid.PageSize); i++) 
         {
             dr = dt.NewRow();

             dr[0] = i;
             dr[1] = "Item " + i.ToString();
             dr[2] = DateTime.Now.ToShortDateString();
             dr[3] = (i % 2 != 0) ? true : false;

             dt.Rows.Add(dr);
         }

         DataView dv = new DataView(dt);
         return dv;

      }

      void Page_Load(Object sender, EventArgs e) 
      {

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {

            // Set the virtual item count, which specifies the total number
            // items displayed in the DataGrid control when custom paging
            // is used.
            MyDataGrid.VirtualItemCount = 200;

            // Retrieve the segment of data to display on the page from the
            // data source and bind it to the DataGrid control.
            BindGrid();

         }

      }

      void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e) 
      {

         // For the DataGrid control to navigate to the correct page when
         // paging is allowed, the CurrentPageIndex property must be updated
         // programmatically. This process is usually accomplished in the
         // event-handling method for the PageIndexChanged event.

         // Set CurrentPageIndex to the page the user clicked.
         MyDataGrid.CurrentPageIndex = e.NewPageIndex;

         // Calculate the index of the first item to display on the page 
         // using the current page index and the page size.
         startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;

         // Retrieve the segment of data to display on the page from the 
         // data source and bind it to the DataGrid control.
         BindGrid();

      }

      void BindGrid() 
      {

         MyDataGrid.DataSource = CreateDataSource();
         MyDataGrid.DataBind();

      }

   </script>

<body>

   <form runat="server">
 
      <h3> DataGrid Custom Paging Example </h3>

      <asp:DataGrid id="MyDataGrid" 
           AllowCustomPaging="True" 
           AllowPaging="True" 
           PageSize="10" 
           OnPageIndexChanged="MyDataGrid_Page" 
           runat="server">

         <HeaderStyle BackColor="Navy" 
                      ForeColor="White" 
                      Font-Bold="True" />

         <PagerStyle Mode="NumericPages" 
                     HorizontalAlign="Right" />

      </asp:DataGrid>

   </form>

</body>
</html>

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

DataGrid-Klasse
DataGrid-Member
System.Web.UI.WebControls-Namespace
VirtualItemCount
CurrentPageIndex
PageSize