【问题标题】:How to do paging in repeater?如何在中继器中进行分页?
【发布时间】:2015-08-29 20:19:53
【问题描述】:

如何对包含 10 条记录的表进行分页以一次只显示其中的一些记录。
后面的代码是:

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();  

    }  

这是html代码:

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

现在我想要下一个和上一个的链接按钮。这显示了下一个和以前的记录。那么我该怎么做呢?

【问题讨论】:

    标签: c# repeater asp.net-4.5


    【解决方案1】:

    您可以添加一个 DataPager 控件。

    不确定您是否可以使用开箱即用的中继器进行寻呼。你需要做很多工作才能让它工作。检查这个link

    您可能想改用 ListView。

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

    【讨论】:

    • @user2069465 使用列表视图而不是中继器。完全删除中继器并修改后面的代码以绑定到列表视图,我已经使用了你的 itemtemplate ,它应该可以正常工作。
    • 但我想通过中继器??
    • @user2069465 我想你应该看看这个链接4guysfromrolla.com/articles/081804-1.aspx :)
    • @lKashef 当然没有难过的感觉!!我只是想听听您的观点.. 如果您注意到.. OP 接受了答案,因为他浏览了我发布的链接并看到了我的意思.. 看看这两个控件.. 非常相似(项目模板)并且易于更改..这就是为什么我建议使用列表视图并提供一个链接以使用中继器,如果他愿意的话。
    • @lKashef 您好,我根据您的反馈更改了答案...谢谢
    猜你喜欢
    • 2010-09-06
    • 1970-01-01
    • 2019-04-23
    • 2019-08-17
    • 2017-10-03
    • 2017-01-20
    • 2012-06-04
    • 2014-05-08
    • 2019-08-22
    相关资源
    最近更新 更多