User Tools

Site Tools


notes:asp.net:repeater

ASP.NET Repeater

Example: A simple repeater generating a list of hyperlink controls:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        &#149; <asp:HyperLink Text='<%# Eval("Product") %>' 
            NavigateUrl='<%# Eval("ID", "details.aspx?ID={0}") %>' 
            runat="server" /><br />
    </ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
    BindRepeater();
}
 
private void BindRepeater()
{
    var products = new[]
    {
        new { ID = 100, Product = "Umbrella" },
        new { ID = 101, Product = "Table" },
        new { ID = 102, Product = "Chair" },
        new { ID = 103, Product = "Lamp" },
        new { ID = 104, Product = "Bookcase" },
    };
 
    Repeater1.DataSource = products;
    Repeater1.DataBind();
}

Example: A Repeater that generates HTML table rows and handles the ItemDataBound event:

<table width="150" border="1" cellpadding="5" cellspacing="0">
    <asp:Repeater id="Repeater1" 
        OnItemDataBound="Repeater1_ItemDataBound" 
        runat="server">
        <HeaderTemplate>
            <tr>
                <td align="center">Product name</td>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>   
                <td><asp:Label id="lblProduct" Text='<%# Eval("ProductName") %>' runat="server" /></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>
protected void Page_Load(object sender, EventArgs e)
{
    BindRepeater();
}
 
private void BindRepeater()
{
    var products = new[]
    {
        new Product { ProductName = "Umbrella", ProductType = 1 },
        new Product { ProductName = "Table", ProductType = 2 },
        new Product { ProductName = "Chair", ProductType = 2 },
        new Product { ProductName = "Lamp", ProductType = 3 },
        new Product { ProductName = "Bookcase", ProductType = 2 },
    };
 
    Repeater1.DataSource = products;
    Repeater1.DataBind();
}
 
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // Obtain an item of the type 'Product'.
        Product item = e.Item.DataItem as Product;
 
        int type = item.ProductType;
        Label lblProduct = e.Item.FindControl("lblProduct") as Label;
 
        switch (type)
        {
            case 1:
                lblProduct.ForeColor = System.Drawing.Color.Blue;
                break;
            case 2:
                lblProduct.ForeColor = System.Drawing.Color.Gray;
                break;
            case 3:
                lblProduct.ForeColor = System.Drawing.Color.Green;
                break;
        }
    }
}
 
...
 
public class Product
{
    public string ProductName { get; set; }
    public int ProductType { get; set; }
}

Example: A Repeater that handles the ItemDataBound and ItemCommand events:

<asp:Repeater ID="Repeater3" 
    OnItemDataBound="Repeater3_ItemDataBound"
    OnItemCommand="Repeater3_ItemCommand"
    runat="server">
    <ItemTemplate>
        <asp:Label ID="lblProduct" Text='<%# Eval("Product") %>' Width="80" runat="server" />
        <asp:LinkButton ID="btnDelete" Text="Delete" CausesValidation="False"
            CommandName="DeleteProduct" CommandArgument='<%# Eval("ID").ToString() %>'
            OnClientClick="return confirm('Are you sure you want to delete this record?')" 
            runat="server" />
        <br />
    </ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
    BindRepeater();
}
 
private void BindRepeater()
{
    var products = new[]
    {
        new { ID = 100, Product = "Umbrella" },
        new { ID = 101, Product = "Table" },
        new { ID = 102, Product = "Chair" },
        new { ID = 103, Product = "Lamp" },
        new { ID = 104, Product = "Bookcase" },
    };
 
    Repeater1.DataSource = products;
    Repeater1.DataBind();
}
 
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        // Hide the Delete button for some products.
 
        // Note that this line of code obtains a value from a data item 
        // without knowing the type of this data item.
        int id = (int)DataBinder.Eval(e.Item.DataItem, "ID");
 
        if (id == 101 || id == 104)
        {
            LinkButton btn = e.Item.FindControl("btnDelete") as LinkButton;
            if (btn != null)
                btn.Visible = false;
        }
    }
}
 
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "DeleteProduct")
    {
        int id = Convert.ToInt32(e.CommandArgument);
 
        // ...
    }
}
notes/asp.net/repeater.txt · Last modified: 2016/04/18 by admin