In this article I will explain how to use and configure ASP.Net DataPager control with code behind DataSource in ASP.Net.
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here
 
HTML Markup
Here I am making use of HTML Table Layout for the ListView control. I have placed the HTML Table inside the

LayoutTemplate with Table Header and then a GroupPlaceHolder and finally the Footer row with a DataPager control in it.
The GroupPlaceHolder will display the contents from GroupTemplate. The GroupTemplate contains a PlaceHolder wrapped within TR Tags.
The PlaceHolder will display the contents from the ItemTemplate where I have placed the TD cells populated from the database.
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>
                CustomerId
            </th>
            <th>
                ContactName
            </th>
            <th>
                Country
            </th>
        </tr>
        <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        <tr>
            <td colspan = "3">
                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="10">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                            ShowNextPageButton="false" />
                        <asp:NumericPagerField ButtonType="Link" />
                        <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" />
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>
<GroupTemplate>
    <tr>
        <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
    </tr>
</GroupTemplate>
<ItemTemplate>
    <td>
        <%# Eval("CustomerId") %>
    </td>
    <td>
        <%# Eval("ContactName") %>
    </td>
    <td>
        <%# Eval("Country") %>
    </td>
</ItemTemplate>
</asp:ListView>
 
 
Associating the DataPager with the ListView control
In order to associate 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. We need to specify the event OnPagePropertiesChanging which will be raised when the DataPager Page is changed or clicked.
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Binding the ListView control
The ListView control is populated in the Page Load event of the page, the records from the Customers Table of the Northwind Database are bound to the ListView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindListView();
    }
}
 
private void BindListView()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "SELECT CustomerId, ContactName, Country FROM Customers";
            cmd.Connection = con;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                lvCustomers.DataSource = dt;
                lvCustomers.DataBind();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindListView()
    End If
End Sub
 
Private Sub BindListView()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand()
            cmd.CommandText = "SELECT CustomerId, ContactName, Country FROM Customers"
            cmd.Connection = con
            Using sda As New SqlDataAdapter(cmd)
                Dim dt As New DataTable()
                sda.Fill(dt)
                lvCustomers.DataSource = dt
               lvCustomers.DataBind()
            End Using
        End Using
    End Using
End Sub
 
 
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 triggered.
Here first the DataPager control is found and then its SetPageProperties method is executed so that the page numbers are recreated.
C#
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    this.BindListView();
}
 
VB.Net
Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
    TryCast(lvCustomers.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
    Me.BindListView()
End Sub
 
Implement Paging in ASP.Net ListView control using DataPager without using DataSource control
 
Demo
 
Downloads