简体   繁体   English

尝试为asp.net中没有数据源控件的列表视图创建datapager时遇到绑定错误

[英]Getting a binding error, when trying to create a datapager for a listview without datasource control in asp.net

I am trying to create a datapager for my list view on the bottom of the page. 我正在尝试为页面底部的列表视图创建datapager。 When i bind the listview without the datapager i get no error and all of the content gets listed like it should. 当我绑定没有datapager的listview时,我没有错误,并且所有内容都按其应有的方式列出。 But when i try to create a datapager i get an error that says: list needs a datasource or need an ICollection as a datasource if AllowPaging is true 但是,当我尝试创建datapager时,出现一条错误消息: 如果AllowPaging为true ,则列表需要数据源或需要ICollection作为数据源

Here is my listview: 这是我的列表视图:

<asp:ListView ID="lstAllProducts" runat="server">
        <ItemTemplate>
            <a style="color: black" href="ProductInfo.aspx?id=<%# Eval("itemId") %>">
                <div class="wrpInnerItem hvr-fade">
                    <div class="innerItem">
                        <p><%#Eval("gtin")%></p>
                    </div>
                    <div class="innerItem">
                        <p><%#Eval("brandName")%></p>
                    </div>
                    <div class="innerItem">
                        <p><%#Eval("functionalName")%></p>
                    </div>
                    <div class="innerItem">
                        <p><%#Eval("unitDescription")%></p>
                    </div>
                    <div class="innerItem">
                        <p><%#Eval("articleNr")%></p>
                    </div>
                </div>
            </a>
        </ItemTemplate>
    </asp:ListView>

And here is the datapager: 这是datapager:

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lstAllProducts" PageSize="12">
        <Fields>
            <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowLastPageButton="true" />
        </Fields>
    </asp:DataPager>

And here is where i bind the data for the listview in the page load: 这是我在页面加载中为listview绑定数据的地方:

if (!IsPostBack)
        {
            lstAllProducts.DataSource = svc.GetAllItemsOverview().OrderByDescending(i => i.id);
            lstAllProducts.DataBind();
        }

I have tried to follow the guide in this link. 我试图按照此链接中的指南进行操作。 http://weblogs.asp.net/hajan/paging-listview-using-datapager-without-using-datasource-control http://weblogs.asp.net/hajan/paging-listview-using-datapager-without-using-datasource-control

hope some of you have a suggestion on why it wont work. 希望你们中的一些人对为什么它不起作用有个建议。

The problem with your code is that DataPager needs complete data for calculating the page numbers but you are assigning IEnumerable<T> to your ListView which will be resolved while binding the data (You can read about LINQ Deferred Execution ). 代码的问题在于, DataPager需要完整的数据来计算页码,但是您正在将IEnumerable<T>分配给ListView ,在绑定数据时将解决该问题(您可以了解LINQ Deferred Execution )。

To solve the issue simply enumerate the results using ToArray or ToList :- 要解决该问题,只需使用ToArrayToList枚举结果:

lstAllProducts.DataSource = svc.GetAllItemsOverview()
                               .OrderByDescending(i => i.id)
                               .ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM