Description:-
In this example we explain that how to use DataPager control
with Listview in Asp.Net
What is DataPager
Why use DataPager
The ListView is
a hybrid control between a DataGrid control and Repeater control that join or
combines the templating of the Repeater control and the editing features of the
data grid control.
We all know that The
ListView control doesn't support paging, so the DataPager serves as an external control to
provide paging facility.
DataPager control
can be associated with the data-bound control by using the PagedControlID
property. Alternatively, by putting the DataPager control inside the data-bound
control hierarchy. For example, in the ListView control, DataPager control can
be kept inside the ListView-LayoutTemplate.
The number of rows or
items that are displayed for each page of data can be customized by changing
the PageSize property of the Datapager control.
Pager Fields
Here is some navigation
property of the DataPager control to
display navigation controls.
Pager
field type
|
Description
|
NextPreviousPagerField
|
Enables
users to navigate through pages one page at a time, or to jump to the first
or last page.
|
NumericPagerField
|
Enables
users to select a page by page number.
|
TemplatePagerField
|
Enables
you to create a custom paging UI.
|
Page Properties
The following table
property escribes the number of rows are displayed in a control.
Property
|
Description
|
MaximumRows
|
The
maximum number of records that are displayed for each page of data.
|
StartRowIndex
|
The
index of the first record that is displayed on a page of data.
|
TotalRowCount
|
The
total number of records that are available in the underlying data source.
|
How to use DataPager
in Asp.Net
In order to use the DataPager
control with the ListView we need to do two things
1. The ID of the ListView
control is set for the PagedControlID property of the DataPager
control.
2. and second is you need to define the event OnPagePropertiesChanging which will be raised or fired
when the DataPager Page is changed or clicked.
You will also need to import the following namespaces.
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Handling Paging in ListView
using DataPager control
When the page is changed or
clicked inside the DataPager the following event hander of the ListView is
fired or triggered.
Here first the DataPager
control is found and then its SetPageProperties method is executed so that the page
numbers are recreated.
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs
e)
{
(lvemp.FindControl("dtpaging") as
DataPager).SetPageProperties(e.StartRowIndex,
e.MaximumRows, false);
this.BindListView();
}