在ASP.NET中限制项目Repeater

时间:2016-07-10 10:53:59

标签: asp.net

您好我想向Repeater展示5件商品。我怎么做? 我不想使用分页。 如何限制Repeater控件? 感谢。

2 个答案:

答案 0 :(得分:0)

将DataSource设置为只有5个项目。

答案 1 :(得分:0)

<强> .ASPX:

<asp:Repeater ID="rptProducts" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' />|
        <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' /><br/>
    </ItemTemplate>
</asp:Repeater>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        GetData();
}

private void GetData()
{
    var p1 = new Product { ID = 1, Description = "Product 1" };
    var p2 = new Product { ID = 2, Description = "Product 2" };
    var p3 = new Product { ID = 3, Description = "Product 3" };
    var p4 = new Product { ID = 4, Description = "Product 4" };
    var p5 = new Product { ID = 5, Description = "Product 5" };
    var p6 = new Product { ID = 6, Description = "Product 6" };

    var products = new List<Product> { p1, p2, p3, p4, p5, p6 };
    rptProducts.DataSource = products.Take(5).ToList();
    rptProducts.DataBind();
}