DataPager无法使用我的数据源

时间:2012-06-29 20:38:28

标签: c# linq

我似乎无法让我的datapager工作。我一直在寻找答案,似乎我得到的错误“ListView with id'searchResults'必须有一个数据源,实现ICollection或者如果AllowPaging为真,可以执行数据源分页。”很常见,通常答案是将ToArray()放在DataSource上。但是我不确定如何将数组放在我的链接语句中。有人可以提供建议。

     searchResults.DataSource = from r in response.Results
        select new
        {
            Title = r[SearchContentProperty.Title],
            Summary = r[SearchContentProperty.HighlightedSummary]
        };

    searchResults.DataBind(); 

2 个答案:

答案 0 :(得分:2)

要将ToArray()添加到LINQ查询,请尝试:

 searchResults.DataSource = (from r in response.Results
    select new
    {
        Title = r[SearchContentProperty.Title],
        Summary = r[SearchContentProperty.HighlightedSummary]
    }).ToArray();

searchResults.DataBind();

答案 1 :(得分:1)

searchResults.DataSource = (from r in response.Results
select new
{
    Title = r[SearchContentProperty.Title],
    Summary = r[SearchContentProperty.HighlightedSummary]
}).ToArray();

searchResults.DataBind(); 是的,

编辑:我遇到了几乎相同的问题,但如果仍然没有分页,你可以使用searchResults PagePropertiesChanging事件并编写这样的代码来获得分页

protected void searchResults_PagePropertiesChanging(object sender,PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

//write your codes again to bind data,for this example:


searchResults.DataSource = from r in response.Results
select new
{
    Title = r[SearchContentProperty.Title],
    Summary = r[SearchContentProperty.HighlightedSummary]
 };

 searchResults.DataBind();
}