开发者

put tag on N'th item by using the asp:repeater

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&g开发者_如何学运维t;
     <%# GetWinnerID(Container.DataItem) %>
     </ItemTemplate>
    <FooterTemplate></FooterTemplate>
</asp:Repeater>


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜