views:

696

answers:

3

I have a method to return a group of objects as a generic list which I then bind to a Repeater. I want to implement paging on the repeater using the PagedDataSource class but I'm not sure that this is possible as it doesn't seem to work.

Will I have to change the return type of my method or is it possible to bind the PagedDataSource to the generic list?

+3  A: 

I've just modified some of my code to use a generic list and seems to have worked fine, hope this helps:

Note that this entire method can be called with or without a page number to automatically set the page, it also builds a paging control inside of a panel calling PagingPanel.

The line that sets the DataSource on the PagedDataSource instance (dataSource) did take an array of NewsItems (searchResults), I've updated it to consume a List that's created using the NewItem array.

void PopulateNewsItems (int? pageNo)
{
    var model = ModelFactory.GetNewsModel ();
    var searchResults = model.GetNewsItems ();

    var dataSource = new PagedDataSource ();

    // CHANGED THE ARRAY OF NEWSITEMS INTO A GENERIC LIST OF NEWSITEMS.
    dataSource.DataSource = new List<NewsItem> (searchResults);
    dataSource.AllowPaging = true;

    var pageSizeFromConfig = ConfigurationManager.AppSettings["NewsItemsPageCount"];
    var pageSize = 10;

    int.TryParse (pageSizeFromConfig, out pageSize);

    dataSource.PageSize = pageSize;
    dataSource.CurrentPageIndex = pageNo ?? 0;

    PagingPanel.Controls.Clear ();
    for (var i = 0; i < dataSource.PageCount; i++)
    {
        var linkButton = new LinkButton ();
        linkButton.CommandArgument = i.ToString ();
        linkButton.CommandName = "PageNo";
        linkButton.Command += NavigationCommand;
        linkButton.ID = string.Format ("PageNo{0}LinkButton", i);
        if (pageNo == i || (pageNo == null && i == 0))
        {
            linkButton.Enabled = false;
            linkButton.CssClass = "SelectedPageLink";
        }

        linkButton.Text = (i + 1).ToString ();

        PagingPanel.Controls.Add (linkButton);
        if (i < (dataSource.PageCount - 1))
            PagingPanel.Controls.Add (new LiteralControl ("|"));
    }

    NewsRepeater.DataSource = dataSource;
    NewsRepeater.DataBind ();
}

void NavigationCommand (object sender, CommandEventArgs e)
{
    PopulateNewsItems (int.Parse ((string)e.CommandArgument));
}
Kieron
Thanks for the help, nice example.
Robert Dougan
No problem, glad it helped.
Kieron
+2  A: 
  1. Set your PagedDataSource's datasource to your list
  2. Setup the paging variables of your PagedDataSource to whatever you require
  3. Set your repeater's datasource to the pageddatasource object itself
  4. Bind the repeater
  5. Job done
John_
A: 

good work. thanks