开发者

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

开发者 https://www.devze.com 2022-12-31 09:02 出处:网络
I want to put <br> tag only in front of 6th item. how can I do this? <asp:Repeater ID=\"rptWinner\" runat=\"server\">

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

精彩评论

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