Access the Repeater.Item.Count value in the codebehind method

I have a relay on my page:

<asp:Repeater id="attachmentsRepeater" runat="server">
    <HeaderTemplate>
        <% 
        if (attachmentsRepeater.Items.Count > 0) {
            if (attachmentsRepeater.Items.Count == 1) {
                Response.Write("<h3>Attachment</h3>");
                Response.Write("<p>");
            } else {
                Response.Write("<h3>Attachments</h3>");
                Response.Write("<ul>");
            }
        }
        %>
    </HeaderTemplate>
    <ItemTemplate>
        <%# OutputAttachment(Container)%>
    </ItemTemplate>  
    <FooterTemplate>
        <% 
        if (attachmentsRepeater.Items.Count > 0) {
            if (attachmentsRepeater.Items.Count == 1) {
                Response.Write("</p>");
            } else {
                Response.Write("</ul>");
            }
        }
        %>
    </FooterTemplate>
</asp:Repeater>

The original ItemTemplate code looked like this:

<ItemTemplate>
    <%
    if (attachmentsRepeater.Items.Count > 0) {
        if (attachmentsRepeater.Items.Count > 1) {
            Response.Write("<li>");
        }
        %>
        <a href="<%# DataBinder.Eval(Container.DataItem, "location") %>">
            <%# DataBinder.Eval(Container.DataItem, "name") %>
        </a>
        <%
        if (attachmentsRepeater.Items.Count > 1) {
            Response.Write("<li>");
        }
    }
    %>
</ItemTemplate>  

In the code, I would like to access the number of elements in the repeater (line 4):

public string OutputAttachment(RepeaterItem Container) {
    string returnValue = "";
    Repeater ContainerParent = (Repeater)Container.Parent;
    if (attachmentsRepeater.Items.Count > 0) {
        if (attachmentsRepeater.Items.Count > 1) {
            returnValue += "<li>";
        }
        returnValue += "<a href=\"" + DataBinder.Eval(Container.DataItem, "location");
        if (DataBinder.Eval(Container.DataItem, "location").ToString().EndsWith("/")) {
            returnValue += DataBinder.Eval(Container.DataItem, "name");
        }
        returnValue += ">" + DataBinder.Eval(Container.DataItem, "name") + "</a>";
        if (attachmentsRepeater.Items.Count > 1) {
            returnValue += "</li>";
        }
    }
    return returnValue;
}

Output code

<h3>Attachment</h3>
<p> </p>

From this conclusion, I know that Item.Count == 1, since there is a conclusion, H3 is the only one and has a P tag. If Item.Count> 1, H3 will be multiple and there will be a UL tag.

Does this codebehind method execute before data binding? Any workarounds for this? Thank you for your help.

This worked for me before, but I had to change it to fulfill the new requirement, which was when it stopped working.

+5
1

- , page_load , .

, , , OuputAttachment , , , .

+1

All Articles