Ryan Ternier

Killer ASP.NET ninja coding monkeys do exist!

Manipulating Data in a Repeater Control

After browsing some of the posts on the asp.net forums a few people asked how to manipulate data in a Repeater Control. Here is a quick sample on how to do this.

A repeater is very powerful tool when you need to well... repeat something many times. If you are going to be repeating tabular data, use a DataGrid, for anything else use a Repeater.

First, A few notes on the Repeater:

The Repeater control allows you to create templates to define the layout of its content. The templates are:

 

  • ItemTemplate - Use this template for elements that are rendered once per row of data.
  • AlternatingItemTemplate- Use this template for elements that are rendered every other row of data. This allows you to alternate background colors, for example.
  • HeaderTemplate- Use this template for elements that you want to render once before your ItemTemplate section.
  • FooterTemplate- Use this template for elements that you want to render once after your ItemTemplate section.
  • SeperatorTemplate- Use this template for elements to render between each row, such as line breaks.

After a repeater has been created, you might end up with something like this:

<asp:Repeater ID="rptApprovesr" runat="server" OnItemCommand="rptApprovesr_ItemCommand">

    <HeaderTemplate>

        <table border="0" cellpadding="0" cellspacing="0">

    </HeaderTemplate>

    <ItemTemplate>

        <tr>

            <td>

                <asp:ImageButton ID="imbDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/button_delete.gif" /><td>

                    <asp:Literal ID="litStaffID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"StaffID") %>' Visible="false" />

                    <asp:Literal ID="litApproverName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FullName") %>' /></td>

                <td>

                    <asp:ImageButton ID="imbMoveDown" CommandName="MoveUp" runat="server" ImageUrl="~/Images/button_Up.gif" /><td>

                    <asp:ImageButton ID="imbMoveUp" CommandName="MoveDown" runat="server" ImageUrl="~/Images/button_down.gif" /><td>

        </tr>

    </ItemTemplate>

    <FooterTemplate>

        </table></FooterTemplate>

</asp:Repeater>

Now that we have a repeater, we might want to manipulate some Data. We can use front end binding like you see above: Text='<%# DataBinder.Eval(Container.DataItem,"FullName") %>' or we can use the ItemDataBound event:

protected void rptApprovesr_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

    if(e.Item.DataItem != null)

    {

        //Change a the staff ID to 1.

        ((Literal)e.Item.FindControl("litStaffID")).Text = "1";

        //Add the Approver Name

        ((Literal)e.Item.FindControl("litApproverName")).Text = ((((System.Data.DataRow)e.Item.DataItem)["ApproverName"]).ToString());

    }

}

The ItemDataBound event is called for each "row" you are adding to the repeater. If your DataSource has 10 rows, this function will be called 11 times (the first time doesn't have a valid DataItem that's why I do the check at the front). You can see above that I can use the FindControl method with the actual control ID passed in to access that control.

I can also declare a control and manipulate it that way If I needed to do more than just change the text:

        ImageButton imgDelete = (ImageButton)e.Item.FindControl("imbDelete");

        imgDelete.CssClass = "Some Style Class";

        imgDelete.AlternateText = "Delete something";

        imgDelete.CommandName = "Delete";

When you do the above, you are creating a pointer reference to the actual object in the repeater. In other words you don't need to "Re-Add" the control to the repeater. Any changes you do to it will automatically be reflected to the current item (row) of the repeater.

Posted: Apr 07 2006, 01:12 PM by Ryan Ternier | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)