C# List View Paging

4/1/2014·3 min read

ListView + DataPager

This's article shows how to use .NET ListView to enable paging, basically, since ListView had imprement IPageableItemContainer, it's able to use DataPager control. You can't use GridView for DataPager since it's not imprement IPageableItemContainer, but if you still want to use DataPager for your paging in GridView, you could write your customized GridView which imprement IPageableItemContainer.

This's example only use following, ListView and DataPager. Because it's not use other objectDataSource like connect to business logic method, or linq, database. You have to bind the actual data either when page first load, or when DataPager's PreLender event.

 

 

aspx page
  <div>
         <asp:ListView ID="ListView1" runat="server">
             <ItemTemplate>
             <div class="list">
                 <p><strong>Product ID</strong> :  <% # Eval("Id") %> </p>
 
                 <strong>Product Description</strong>:  <% # Eval("DescTest") %>
 
                <p><strong>Product Price</strong>:  <% # Eval("PriceTest") %> </p>
             </div>
             </ItemTemplate>
         </asp:ListView>
     <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"
         onprerender="DataPager1_PreRender" PageSize="5">
         <Fields>
             <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                 ShowNextPageButton="False" ShowPreviousPageButton="False" />
             <asp:NumericPagerField  />
             <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
                 ShowNextPageButton="False" ShowPreviousPageButton="False"  />
         </Fields>
     </asp:DataPager>
     </div>
 
aspx server side code
  protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 ListView1.DataSource = new BusinessLogic().GetProductData();
                 ListView1.DataBind();
             }
         }
 
         protected void DataPager1_PreRender(object sender, EventArgs e)
         {
             ListView1.DataSource = new BusinessLogic().GetProductData();
             ListView1.DataBind();
         }