简体   繁体   中英

How to add paging capabilities to the Repeater control in ASP.Net?

I have an asp.net Repeater control .I have to display only 5 rows in the repeater. I searched about it in Google but most of the resources are talking about how to do it with DataSet. In my case, I am using SqlDataSource. So I can have paging in this Repeater control?

ASP.NET Code:

<asp:Repeater ID="Repeater3" runat="server" DataSourceID="SqlDataSource3">
                    <HeaderTemplate>
                        <div>
                        <table border="1">
                            <thead>
                                <tr>
                                    <td colspan="3">
                                        <center> <strong>Safety Quizzes Record</strong> </center>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <center> <strong>Quiz Title</strong> </center>
                                    </td>
                                    <td>
                                        <center> <strong>Date & Time</strong> </center>
                                    </td>
                                    <td>
                                        <center> <strong>Score</strong> </center>
                                    </td>
                                </tr>
                            </thead>

                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <p>
                                    <%# Eval("Title") %>
                                </p>
                            </td>
                            <td>
                                <p>
                                    <%# Eval("DateTimeComplete") %>
                                </p>
                            </td>
                            <td>
                                <p>
                                    <%# Eval("Score") %>
                                </p>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                        </div>
                    </FooterTemplate>
                </asp:Repeater>
                <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     dbo.Quiz.Title, dbo.UserQuiz.DateTimeComplete, dbo.UserQuiz.Score
FROM         dbo.employee INNER JOIN
                      dbo.UserQuiz ON dbo.employee.Username = dbo.UserQuiz.Username INNER JOIN
                      dbo.Quiz ON dbo.UserQuiz.QuizID = dbo.Quiz.QuizID
WHERE     (dbo.employee.Username = @Username)">
                    <SelectParameters>
                        <asp:Parameter Name="Username" />
                    </SelectParameters>
                </asp:SqlDataSource>

UPDATE:

What I want is showing only 5 items in the Repeater and the others will be listed on the other pages of the Repeater. So how I can have Pagining with SqlDataSource as mentioned above.

The Repeater control does not support paging out-of-the-box.

You could however implement it but it would require you to manually code it.

I would like to recommend you to change the Repeater control for the ListView control

Differences:

  • Repeater . This control does not support paging, updating, inserting or deleting operations out-of-the-box, if you want these features you would need to manually code them.

  • ListView . This is the most flexible of all data-bound controls. It's based on templates like the Repeater and DataList controls. It does support CRUD operations automatically mapping the operations agains your data source control. It does support server paging, and you can customize pretty much everything

BTW, since you are using a SqlDataSource , you would have to manually add paging code to your queries or store procedures, you could change that behavior if you use a LinqDataSource or an EntityDataSource

This is a simplified ListView markup:

    <asp:ListView runat="server" ID="lv"
        DataSourceID="lds" DataKeyNames="emp_id"
        EnableModelValidation="false"
        EnablePersistedSelection="false" 
    >
        <LayoutTemplate>
            <br />
            <br />
            A static header
            <div runat="server" id="itemPlaceHolder"></div>
            <br />
            <asp:DataPager runat="server" PageSize="4">
                <Fields>
                    <asp:NextPreviousPagerField 
                        ButtonType="Button" 
                        ShowFirstPageButton="true"
                        ShowLastPageButton="true"
                    />
                </Fields>
            </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
            <div style="background-color:Gray">
                First Name
                <div><asp:Label runat="server" ID="lbl" Text='<%# Eval("fname") %>'></asp:Label></div>
                <br />
                <asp:GridView ID="GridView1" runat="server" DataSource='<%# Eval("Achivements") %>' />
                <br />
                <asp:LinkButton ID="LinkButton1" Text="Select" CommandName="Select" runat="server" />
                <asp:LinkButton ID="LinkButton2" Text="Edit" CommandName="Edit" runat="server" />
                <asp:LinkButton ID="LinkButton4" Text="Delete" CommandName="Delete" runat="server" />
            </div>
        </ItemTemplate>
        <ItemSeparatorTemplate>
            <hr />
        </ItemSeparatorTemplate>
    </asp:ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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