Wednesday, December 11, 2013

Paging with a ListView instead of a Repeater

tI came across a task which required the functionality of a repeater (the ability to bind data and have it display in an HTML item template), but also the ability to page through the data (much like a GridView). There were some solutions out there, which required a lot of customization and involved too much time to make it worthwhile.

The ListView control however allows the display of bound data in an HTML template as well as other features such as: sorting, data editing, data insertions, and data deletions.

The following code is the basic structure for the ListView. The placeholder is where each item template will be placed (which is handy when you want a div or other HTML element to wrap around the data items). You can then add the DataPager, which allows for the ListView paging. You will also need the page properties changing event to ensure the paging works seamlessly.
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="10">
        <asp:NumericPagerField ButtonType="Link" />
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    //set current page startindex, max rows and rebind to false
    lvDataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

    // bind data to list view
<asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
<%# Eval("DataField") %>
No data to display

No comments:

Post a Comment