Search code examples
c#.netdatarepeater

How to hide() first element in a <asp:repeater>


This is my code :

<asp:Repeater runat="server" ID="rpUbicazione">
    <ItemTemplate>
        <div class="Field" style="margin-bottom:20px;">
            // elements
        </div>
    </ItemTemplate>
</asp:Repeater>

and I'd like to hide first element. So I tried changing first line with :

<asp:Repeater runat="server" ID="rpUbicazione" Visible="<%# (Container.ItemIndex != 0) %>">

but seems it doesnt works : ItemIndex it is not a method.

How can I do it?


Solution

  • Try this:

    <asp:Repeater runat="server" ID="rpUbicazione">
        <ItemTemplate>
            <div class="Field" style='margin-bottom: 20px; display: <%# Container.ItemIndex == 0 ? "none" : "block"  %>'>
                // elements
            </div>
        </ItemTemplate>
    </asp:Repeater>
    

    or you can do something like this:

    <script runat="server">
        protected void rpUbicazione_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex == 0)
            {
                e.Item.FindControl("divElement").Visible = false;
            }
        }
    </script>
    
    <asp:Repeater runat="server" ID="rpUbicazione" onitemdatabound="rpUbicazione_ItemDataBound">
        <ItemTemplate>
            <div id="divElement" runat="server" class="Field" style="margin-bottom: 20px;">
                // elements
            </div>
        </ItemTemplate>
    </asp:Repeater>