How to Bind Data to DataPager inside ListView Control in ASP.NET?

In ASP.NET there are several controls to present data in a web page. Some of the popular data controls are GridView, ListView, DetailsView, FormView, Repeater or SQLDataSource. If you will notice in the recent versions of Visual Studio you will watch DataPager as a Data Control. Like a Gridview using DataPager Control we can present data in tabular shape. Look at the example below where I am binding data to a DataPager control inside a ListView Control.

To enable users to page through data in a ListView control or a control which implements IPageableItemContainer interface, we can use DataPager control in ASP.NET. While declaring a DataPager Control declare it inside LayoutTemplate. If your DataPager Control is not inside the ListView control then you need to set PagedControlID property value to ListView control ID.

ListView from Default.aspx

<asp:ListView ID="lvEmployees" runat="server" GroupPlaceholderID="grpPlaceHoder"
ItemPlaceholderID="itemPlaceHolder" OnPagePropertiesChanging="OnPropertiesChanging">
<table cellpadding="0" cellspacing="0">
<th>Emp ID</th>
<th>Emp Name</th>
<asp:PlaceHolder runat="server" ID="grpPlaceHoder"></asp:PlaceHolder>
<td colspan="3">
<asp:DataPager ID="dpager" runat="server" PagedControlID="lvEmployees" PageSize="10">
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
<td><%# Eval("ID")%></td>
<td><%# Eval("Name")%></td>
<td><%# Eval("Status") %></td>

Code-behind Default.aspx.vb

Imports System.IO
Imports System.Data.OleDb
Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page

Dim FilePath As String = "C:\Users\biswabhusan_panda\Documents\Visual Studio 2013\WebSites\ExcelApp\Files\temp.xlsx"
Dim Extension As String = ".xlsx"

Dim ConStr As String = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString()

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView(FilePath, Extension)
End If
End Sub

Protected Sub OnPropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
TryCast(lvEmployees.FindControl("dpager"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
Me.BindListView(FilePath, Extension)
End Sub

Private Sub BindListView(ByVal FilePath As String, ByVal Extension As String)

ConStr = String.Format(ConStr, FilePath)

Dim connExcel As New OleDbConnection(ConStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dtbl As New DataTable()

cmdExcel.Connection = connExcel

'Get the name of First Sheet 

Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()


'Read Data from First Sheet 

cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"

oda.SelectCommand = cmdExcel

'Bind Data to GridView 
lvEmployees.DataSource = dtbl

End Sub
End Class