5

I have a page with a listview control and a datapager control. The listviews datasource is set programatically using this code:

Dim dal as new dalDataContext
Dim bookmarks = From data In dal.getData(userid)
listview1.DataSource = bookmarks
listview1.DataBind()

When i test this page in a browser it comes up with the error: 'ListView with id 'listview1' must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true.'

How can i implement paging in this scenario?

Thanks

j82374823749
  • 847
  • 4
  • 15
  • 24

3 Answers3

18

Try

listview1.DataSource = bookmarks.ToArray()

I had the same problem this week.

KClough
  • 2,079
  • 2
  • 21
  • 29
  • Thankyou so much! Very quick response. Cannot believe it was something that simple. – j82374823749 Jul 30 '09 at 16:58
  • One minor problem, i have to click the page numbers twice before it goes to the next page or previous page. Any idea how to solve this? Thanks. – j82374823749 Jul 30 '09 at 17:02
  • 1
    Better yet use `ToArray()` - it's slightly more lightweight if you don't actually need full functionality of `List` (such as adding new items) - which you do not here; and arrays implement `ICollection` (and `IList`) otherwise. – Pavel Minaev Jul 30 '09 at 17:07
  • briggins5, sounds like a problem with the code that keeps track of the current page/binds data, best suited for another post if you're still stuck – KClough Jul 30 '09 at 20:48
  • Thanks for the help guys. I'll ask a new question if i can't fix it myself. – j82374823749 Jul 30 '09 at 21:43
2

An answer to the click-twice problem that the OP subsequently encountered - move the Databind to the OnPreRender event handler:

    protected void Page_PreRender(object sender, EventArgs e)
    {
        listview1.DataBind();
    }
CJM
  • 11,908
  • 20
  • 77
  • 115
0

or maybe create a page properties changing and bindlistview there.

protected void lv_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    //set current page startindex, max rows and rebind to false
    DataPager dp = lvNews.FindControl("lvDataPager1") as DataPager;
    dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

    BindListView();
}
lucky
  • 1