How to do pagination in the ListView using DataPager control?

Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 14123 red flag

To list the paginated records in the ListView, we can follow this approach.


ListView control is the new and improved control introduced from ASP.NET 3.5 version. It is a template based control that gives us better command over the layout. It can also be looked as an alternative to the DataList control.

Get hundreds of ASP.NET Tips and Tricks and ASP.NET Online training here.

In the previous article, we learnt about how to perform CRUD (Create Read Update & Delete) operation in ListView.
In this article, we shall learn how to do pagination in the ListView using DataPager control?


<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="ChangePage">


<table border="1">

<tr><th>AutoId</th><th>First Name</th><th>Last Name</th><th>Age</th><th>Active</th></tr>

<asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>






<%# Eval("AutoID") %>



<%# Eval("FirstName") %>



<%# Eval("LastName") %>


<td><%#Eval("Age") %></td>


<%# Eval("Active") %>





<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5">


<asp:NumericPagerField ButtonCount="5" />




string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)


if (!IsPostBack)






private void PopulateData()


DataTable table = new DataTable();

using (SqlConnection conn = new SqlConnection(_connStr))


string sql = "Select * from PersonalDetail";

using (SqlCommand cmd = new SqlCommand(sql, conn))


using (SqlDataAdapter ad = new SqlDataAdapter(cmd))






ListView1.DataSource = table;




/// <summary>

/// Changes the page.

/// </summary>

/// <param name="sender">The sender.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.PagePropertiesChangingEventArgs"/> instance containing the event data.</param>

protected void ChangePage(object sender, PagePropertiesChangingEventArgs e)


DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);




DataPager control:

To paginate the asp:ListView, asp:DataPager control is used. DataPager works with web controls which implements IPageableItemContainer and .NET framework provides only one such control i.e. ListView. Other controls like GridView can be customized to implement this interface so that datapager can be used with them. To know more about asp:DataPager control visit

In the above code snippet, on the .aspx page we have a ListView and a DataPager control. ListView control is attached with DataPager control using PagedControlID property and when the page numbers of DataPager control is clicked the OnPagePropertiesChanging event of ListView fires. In this event, we have specified ChangePage server side method to fire where we have specified the start index, maximum rows per page (these properties are set into the DataPager control) in the ListView and whether to rebind the data after page properties are set or not.

Clicking on page number paginates the records of the ListView.


Thanks for reading, hope you liked it.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: SheoNarayan on: 11/2/2011 | Points: 25
Thanks Vishwas for pointing out the DataPager control statement.

Keep up the good work !


Login to post response

Comment using Facebook(Author doesn't get notification)