The ASP.NET control has a built-in paging capability that supports basic paging functionality. You can use the default paging user interface (UI) or create a custom paging interface.
How Paging Works in the GridView Control
The GridView control supports paging over the items in its data source. You set the property to true to enable paging. The GridView control supports paging in one of these ways:
If the GridView control is bound to a data source control that supports the paging capability at the interface level, the GridView control will take advantage of that capability directly. Paging at the interface level means that the GridView control requests only as many records from the data source as are needed to render the current page. The number of records requested may vary depending on other factors, such as whether the data source supports getting the total record count, the number of records per page specified by the property, and the number of pager buttons to display when the property is set to Numeric.
Of the data source controls that are included in the .NET Framework, only the control supports paging at the interface level.
If the GridView control is bound to a data source control that does not support the paging capability directly, or if the GridView control is bound to a data structure in code through the property, the GridView control will perform paging by getting all of the data records from the source, displaying only the records for the current page, and discarding the rest. This is supported only when the data source for the GridView control returns a collection that implements the interface (including datasets).
If the data source does not support paging directly and does not implement the ICollection interface, the GridView control cannot page. For example, if you are using a control and have set its property to , the GridView control cannot implement paging.
Customizing the Paging Settings and User Interface
You can customize the paging user interface of the GridView control in a number of ways. You can set the size of the page (that is, the number of items to display at once) using the PageSize property. You can also set the current page of the GridView control by setting the property. You can specify more custom behavior using either the property or by supplying a pager template.
The PagerSettings property allows you to customize the appearance of the paging user interface (UI) that is automatically generated by the GridView control when you set the AllowPaging property to true. The GridView control can display direction controls that allow forward and backward navigation as well as numeric controls that allow a user to move to a specific page.
The PagerSettings property of the GridView control is set to a class. You can customize the paging mode by setting the property of the GridView control. For example, you can customize the paging UI mode by setting it as follows:
GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast
The available modes are:
Pager Control Appearance
The GridView control has numerous properties that you can use to customize the text and images for different pager modes. For example, if you want to allow navigation using direction buttons but want to customize the text that appeared, you can customize the button text by setting the and properties, as in the following example:
GridView1.PagerSettings.NextPageText = "Click for next page" GridView1.PagerSettings.PreviousPageText = "Click for previous page"
You can also use images to customize the appearance of your paging controls. The PagerSettings class includes image URL properties for the first, last, previous, and next page command buttons.
Finally, you can control the appearance of the paging commands by setting the property of the GridView control to a value.
Data Paging Template
If you set the AllowPaging property of the GridView control to true, the GridView control automatically adds user interface (UI) controls for paging. You can customize the UI for paging by adding a template. To specify which paging operation to perform, include a control with its property set to Page and a set to one of the following values:
FirstВ В В To move to the first page.
LastВ В В To move to the last page.
PrevВ В В To move to the previous page.
NextВ В В To move to the next page of data
A numberВ В В To move to a specific page.
The GridView control raises two events when it moves to a new page of data. The event occurs before the GridView control performs the paging operation. The event occurs after the new page of data has been returned to the GridView control.
You can use the PageIndexChanging event to cancel the paging operation, if needed, or to perform a task before the GridView control requests a new page of data. You can use the PageIndexChanged event to perform a task after the user moves to a different page of data.