Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm making page with list of articles. I'm using listview for it. It works great. But I want to use pager. But there is error and I'm bit lost.

XML
<asp:ListView runat="server" ID="articlesListView" DataSourceID="SqlDataSource1"
    ItemContainerId="DataSection">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
    </LayoutTemplate>

    <ItemTemplate>
        <div class="article">
            <div class="article-headline">
                <b><asp:Label ID="nameOfArticle" runat="Server" Text='<%# Eval("name") %>' CssClass="article-headline-text"/></b><br />
            </div>
            <div class="article-content">
                <asp:Label ID="contentOfArticle" runat="Server" Text='<%# Eval("content").ToString().Substring(0, Math.Min(Eval("content").ToString().Length, 300)) + "..." %>' CssClass="article-content-text"/><br/><br/>
            </div>
            <div class="article-info">
                <asp:Label ID="dateOfCreation" runat="Server" Text='<%# Eval("date", "{0:d}") %>' CssClass="article-info-text"/><br/><br/>
                <asp:Label ID="author" runat="server" Text='<%# Eval("author") %>' CssClass="article-info-text"/><br/><br/>
                <b><asp:HyperLink ID="sendIdForArticle" runat="server" Text="Číst vše" NavigateUrl='<%#"Article.aspx?id=" + Eval("id") %>' CssClass="article-info-link" /></b><br/>
            </div>
        </div>
    </ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataReader"
    ConnectionString="<%$ ConnectionStrings:DataServices%>" SelectCommand="SELECT id, name, content, date, author, tags, is_published, section_id, category_id, is_deleted FROM articles WHERE [is_deleted]=0 AND is_published=1 ORDER BY date DESC">
</asp:SqlDataSource>


This part is ok. It works great. But when I use datapager:

XML
<asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="articlesListView"
     PageSize="3"  >
     <Fields>
         <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="true" ShowLastPageButton="True" ShowPreviousPageButton="true" />
     </Fields>
 </asp:DataPager>


There is error: ListView with id 'articlesListView' must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true.

I don't know how to move with it. I found something about databind it first. But rly don't know how. Rly need some asskick to boost me.

Thanks a lot!
Posted

do this with out a controlID:
XML
<asp:ListView ID="orders" DataSourceID="dsordes" runat="server">
        <LayoutTemplate>
            <ol id="itemPlaceholder" runat="server"></ol>
            <asp:DataPager ID="pg" PageSize="2" runat="server">
                <Fields>
                    <asp:NextPreviousPagerField
                    ShowFirstPageButton="True"
                    ShowPreviousPageButton="True"
                    ShowNextPageButton="True"
                    ShowLastPageButton="True" />
                    <asp:NumericPagerField />
                </Fields>
            </asp:DataPager>
        </LayoutTemplate>
        
        <ItemTemplate>
            <li>
                <%#Eval("status_id") %>
            </li>
        </ItemTemplate>
    </asp:ListView>

you just a layout tamplate with a control ID equals to itemPlaceholder.
 
Share this answer
 
It is better to use use ToArray() because it's slightly more lightweight if you don't actually need full functionality of List (such as adding new items). Arrays implement ICollection (and IList) otherwise.

try the below code
C#
articlesListView.DataSource = urDataSource.ToArray()
 
Share this answer
 
articlesListView.DataSource = SqlDataSource1.... doesnt work, cause it can be just .ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900