簡體   English   中英

嘗試為asp.net中沒有數據源控件的列表視圖創建datapager時遇到綁定錯誤

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

我正在嘗試為頁面底部的列表視圖創建datapager。 當我綁定沒有datapager的listview時,我沒有錯誤,並且所有內容都按其應有的方式列出。 但是,當我嘗試創建datapager時,出現一條錯誤消息: 如果AllowPaging為true ,則列表需要數據源或需要ICollection作為數據源

這是我的列表視圖:

<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>

這是datapager:

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

這是我在頁面加載中為listview綁定數據的地方:

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

我試圖按照此鏈接中的指南進行操作。 http://weblogs.asp.net/hajan/paging-listview-using-datapager-without-using-datasource-control

希望你們中的一些人對為什么它不起作用有個建議。

代碼的問題在於, DataPager需要完整的數據來計算頁碼,但是您正在將IEnumerable<T>分配給ListView ,在綁定數據時將解決該問題(您可以了解LINQ Deferred Execution )。

要解決該問題,只需使用ToArrayToList枚舉結果:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM