asp.netvb.netlistviewpagination

ListView with DataPager not working


From everything I've read, it seemed that adding paging to a ListView control should be dead simple, but it's not working for me. After adding the ListView and DataPager controls to the form and wiring them together, I'm getting very odd behavior. The DataPager correctly limits the ListView's page size, but clicking the paging buttons doesn't affect the ListView at all. The paging buttons seem to think they are doing they're job, as the last button is disabled when you go to the last page, etc., but the ListView never changes. Also, it takes two clicks on the DataPager to get it to do anything, i.e., clicking on Last once does nothing, but clicking it a second time causes the DataPager to react as if the last page is now selected.

The only thing I can think of is that I'm binding the DataSource at runtime (to a LINQ object), not using a LinqDataSource control or anything. Has anyone seen this behavior? Am I doing something wrong? Here's the code I'm using:

<asp:DataPager ID="HistoryDataPager" runat="server" PagedControlID="HistoryListView" PageSize="10">
    <Fields>
        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="true" />
    </Fields>
</asp:DataPager>

<asp:ListView ID="HistoryListView" runat="server">
    ...
</asp:ListView>

In the code-behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        HistoryListView.DataSource = From x in myContext.myTables ...
        DataBind()
    End If

End Sub

Solution

  • Take a look at the ListViewPagedDataSource.

    private ListViewPagedDataSource GetProductsAsPagedDataSource(DataView dv)
    {
    // Limit the results through a PagedDataSource
    ListViewPagedDataSource pagedData = new ListViewPagedDataSource();
    pagedData.DataSource = dv;
    pagedData.MaximumRows = dv.Table.Rows.Count;
    pagedData.TotalRowCount = dpTop.PageSize;
    
    if (Request.QueryString[dpTop.QueryStringField] != null)
      pagedData.StartRowIndex = (Convert.ToInt32(Request.QueryString[dpTop.QueryStringField]) - 1) * dpTop.PageSize;
    else
      pagedData.StartRowIndex = 0;
    
    return pagedData;
    }
    

    Though, I have a problem viewing the last page. The DataPager jumps back to the first page, but the data displayed is the last page.