Overview of ListView and DataPager in ASP.Net


The ASP.NET ListView control binds and displays the data item returned from a data source. The ListView control displays the data in a format that we define using various templates and styles. If the data item is very large then sometimes we need to display data item in pages. To display the data item in pages (for paging) we use another ASP.NET control DataPager with ListView control.


The DatePager control provides paging for data-bound ASP.NET controls that implement the IPageableItemContainer interface, such as the ListView control.


  1. Create a sample ASP.NET Website, add a new ASPX page.
  2. Add a ListView control to the page and modify the markup in the following way:
    1. <asp:ListView ID="ListView1" runat="server">  
    2.     <ItemTemplate>  
    3.         <div style="background: lightGray">  
    4.             <asp:Label runat="server" ID="Label3" Text='<%#Eval("ID") %>'></asp:Label>  
    5.             <asp:Label runat="server" ID="Label1" Text='<%#Eval("Name") %>'></asp:Label>  
    6.             <asp:Label runat="server" ID="Label2" Text='<%#Eval("Mobile") %>'></asp:Label>  
    7.             <asp:Label runat="server" ID="lblName" Text='<%#Eval("College") %>'></asp:Label>  
    8.             <br />  
    9.         </div>  
    10.     </ItemTemplate>  
    11.     <AlternatingItemTemplate>  
    12.         <div>  
    13.             <asp:Label runat="server" ID="Label3" Text='<%#Eval("ID") %>'></asp:Label>  
    14.             <asp:Label runat="server" ID="Label1" Text='<%#Eval("Name") %>'></asp:Label>  
    15.             <asp:Label runat="server" ID="Label2" Text='<%#Eval("Mobile") %>'></asp:Label>  
    16.             <asp:Label runat="server" ID="lblName" Text='<%#Eval("College") %>'></asp:Label>  
    17.             <br />  
    18.         </div>  
    19.     </AlternatingItemTemplate>  
    20. </asp:ListView>  
  3. Add a DataPager control to the page and modify the markup in the following way:
    1. <asp:DataPager ID="DataPager1" PagedControlID="ListView1" PageSize="4" runat="server">  
    2.     <Fields>  
    3.         <asp:NumericPagerField ButtonType="Link" />  
    4.     </Fields>  
    5. </asp:DataPager>  
    We add the DataPager control, add the PageSize property (want to display at a time) and associate it with the ListView1 control. We define a <field> where we use the field type as NumericPagerField and ButtonType as Link.
  4. The following  is the C# code for adding the custom DataTable (you can bind the data from the data source).
    1. DataTable dt;  
    2. protected void Page_Load(object sender, EventArgs e)  
    3. {  
    4.     if (!IsPostBack)  
    5.     {  
    6.         BindListView();  
    7.     }  
    8. }  
    9. protected void BindListView()  
    10. {  
    11.     dt = new DataTable();  
    12.     dt.Columns.Add("Id"typeof(int));  
    13.     dt.Columns.Add("Name"typeof(string));  
    14.     dt.Columns.Add("Mobile"typeof(string));  
    15.     dt.Columns.Add("College"typeof(string));  
    16.     dt.Rows.Add(1,"Rahul""8505012345""MITRC");  
    17.     dt.Rows.Add(2,"Pankaj""8505012346""MITRC");  
    18.     dt.Rows.Add(3,"Sandeep""8505012347""MITRC");  
    19.     dt.Rows.Add(4,"Sanjeev""8505012348""MITRC");  
    20.     dt.Rows.Add(5,"Neeraj""8505012349""MITRC");  
    21.     dt.AcceptChanges();  
    22.     ListView1.DataSource = dt;  
    23.     ListView1.DataBind();  
    24. }  

Now run the Website and see that only the first four items will be shown. The DataPager has two pages (1 and 2) and you will be able to click the second page to see the last item.

click the second page

Now if we click on page 2, you will see it won't display the second page automatically, you will need to click again on the page 2 link. After double clicking on page 2 you will encounter another problem, the data is not displayed correctly.

data is not displayed correctly

To resolve this problem we need to set the DataPager Page Properties in the ListViews.PagePropertiesChanging event.

First add onpagepropertieschanging events to the ListView control.

  1. <asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging">  
  2. <ItemTemplate>  
Then implement the ListView1_PagePropertiesChanging method.
  1. protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)  
  2. {  
  3.     // set current page startindex,max rows and rebind to false  
  4.     DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);  
  5.     // Rebind the ListView1  
  6.     BindListView();  
  7. }  
Now, you test the functionality, it should work properly.