views:

35

answers:

2

hello.

I want to put <br> tag only in front of 6th item.

how can I do this?

<asp:Repeater ID="rptWinner" runat="server">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
     <%# GetWinnerID(Container.DataItem) %>
     </ItemTemplate>
    <FooterTemplate></FooterTemplate>
</asp:Repeater>
A: 

First you'll need to add a placeholder to hold the BR, or add a label. Then you'll need to tie into the OnItemDataBound event and do checking:

protected void rptWinner_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.Index == 6)
    {
        PlaceHolder ph = e.Item.FindControl("myPlaceholder") as PlaceHolder;
        ph.Controls.Add(new LiteralControl("<br>"));
    }
}

Or something to that effect ;)

JustLoren
+1  A: 

Try this:

<asp:Repeater ID="rptWinner" runat="server">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
         <%# GetWinnerID(Container.DataItem) %>
         <%# Container.ItemIndex == 4 ? "<br />" : string.Empty %>
     </ItemTemplate>
    <FooterTemplate></FooterTemplate>
</asp:Repeater>

this adds <br /> tag after the fifth element (in front of 6th).

Alex
DataItemIndex => ItemIndex ,right?
sunglim
@sunglim, yep, sure! `Container.DataItemIndex` is for `GridView` control. Thanks!
Alex