Search code examples
c#repeaterasp.net-4.5

How to do paging in repeater?


How to do paging for a table of 10 records to show only some of them at once.
The code behind is:

protected void Page_Load(object sender, EventArgs e)  
    {  
        Rep_Bind();  
    }  
    private void Rep_Bind()  
    {  
        SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook",ConfigurationManager.ConnectionStrings["cn"].ConnectionString);  
        DataSet ds = new DataSet();  
        adp.Fill(ds);  
        Repeater1.DataSource = ds;  
        Repeater1.DataBind();  

    }  

And this is the html code:

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <img src='<%#Eval("bookimg") %>'height="50" width="50" />
                            <b>Title:</b><%#Eval("booktit" )%><br />
                            <b>Author:</b><%#Eval("bookauth") %><br />
                            <b>Publisher:</b><%#Eval("bookpub") %><br />
                            <b>Price:</b><%#Eval("bookprc") %>
                        </td>
                    </tr>
                </table>

            </ItemTemplate>
        </asp:Repeater>

Now i want linkbuttons for next and prev. which shows next and previous records.So what should i do for this?


Solution

  • You could add a DataPager control.

    Not sure you can get paging with a Repeater out of the box though. You'll require quite a bit of work to get it working. Check this link

    You might want to use a ListView instead.

    <asp:ListView ID="ListView1" runat="server">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <img src='<%#Eval("bookimg") %>' height="50" width="50" />
                                    <b>Title:</b><%#Eval("booktit" )%><br />
                                    <b>Author:</b><%#Eval("bookauth") %><br />
                                    <b>Publisher:</b><%#Eval("bookpub") %><br />
                                    <b>Price:</b><%#Eval("bookprc") %>
                                </td>
                            </tr>
                        </table>
    
                    </ItemTemplate>
                </asp:ListView>
                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                    </Fields>
                </asp:DataPager>