可以将PagedDataSource绑定到通用列表吗?

时间:2021-08-07 20:54:05

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.

我有一个方法将一组对象作为通用列表返回,然后我将其绑定到Repeater。我想使用PagedDataSource类在转发器上实现分页,但我不确定这是否可行,因为它似乎不起作用。

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

我是否必须更改我的方法的返回类型,或者是否可以将PagedDataSource绑定到通用列表?

2 个解决方案

#1


4  

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.

请注意,可以使用或不使用页码来调用整个方法来自动设置页面,还可以在调用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.

在PagedDataSource实例(dataSource)上设置DataSource的行确实采用了一个NewsItems(searchResults)数组,我已经更新它以使用使用NewItem数组创建的List。

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));
}

#2


3  

  1. Set your PagedDataSource's datasource to your list
  2. 将PagedDataSource的数据源设置为列表

  3. Setup the paging variables of your PagedDataSource to whatever you require
  4. 将PagedDataSource的分页变量设置为您需要的任何内容

  5. Set your repeater's datasource to the pageddatasource object itself
  6. 将转发器的数据源设置为pageddatasource对象本身

  7. Bind the repeater
  8. 绑定中继器

  9. Job done

#1


4  

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.

请注意,可以使用或不使用页码来调用整个方法来自动设置页面,还可以在调用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.

在PagedDataSource实例(dataSource)上设置DataSource的行确实采用了一个NewsItems(searchResults)数组,我已经更新它以使用使用NewItem数组创建的List。

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));
}

#2


3  

  1. Set your PagedDataSource's datasource to your list
  2. 将PagedDataSource的数据源设置为列表

  3. Setup the paging variables of your PagedDataSource to whatever you require
  4. 将PagedDataSource的分页变量设置为您需要的任何内容

  5. Set your repeater's datasource to the pageddatasource object itself
  6. 将转发器的数据源设置为pageddatasource对象本身

  7. Bind the repeater
  8. 绑定中继器

  9. Job done