title: Access the Repeater.Item.Count value within a codebehind method
body: <p>I have a repeater on my page:</p>
  
  <pre><code>&lt;asp:Repeater id="attachmentsRepeater" runat="server"&gt;
      &lt;HeaderTemplate&gt;
          &lt;% 
          if (attachmentsRepeater.Items.Count &gt; 0) {
              if (attachmentsRepeater.Items.Count == 1) {
                  Response.Write("&lt;h3&gt;Attachment&lt;/h3&gt;");
                  Response.Write("&lt;p&gt;");
              } else {
                  Response.Write("&lt;h3&gt;Attachments&lt;/h3&gt;");
                  Response.Write("&lt;ul&gt;");
              }
          }
          %&gt;
      &lt;/HeaderTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;%# OutputAttachment(Container)%&gt;
      &lt;/ItemTemplate&gt;  
      &lt;FooterTemplate&gt;
          &lt;% 
          if (attachmentsRepeater.Items.Count &gt; 0) {
              if (attachmentsRepeater.Items.Count == 1) {
                  Response.Write("&lt;/p&gt;");
              } else {
                  Response.Write("&lt;/ul&gt;");
              }
          }
          %&gt;
      &lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>The original ItemTemplate code looked like this:</p>
  
  <pre><code>&lt;ItemTemplate&gt;
      &lt;%
      if (attachmentsRepeater.Items.Count &gt; 0) {
          if (attachmentsRepeater.Items.Count &gt; 1) {
              Response.Write("&lt;li&gt;");
          }
          %&gt;
          &lt;a href="&lt;%# DataBinder.Eval(Container.DataItem, "location") %&gt;"&gt;
              &lt;%# DataBinder.Eval(Container.DataItem, "name") %&gt;
          &lt;/a&gt;
          &lt;%
          if (attachmentsRepeater.Items.Count &gt; 1) {
              Response.Write("&lt;li&gt;");
          }
      }
      %&gt;
  &lt;/ItemTemplate&gt;  
  </code></pre>
  
  <p>In the codebehind, I would like to access the number of Items in the Repeater (line 4):</p>
  
  <pre><code>public string OutputAttachment(RepeaterItem Container) {
      string returnValue = "";
      Repeater ContainerParent = (Repeater)Container.Parent;
      if (attachmentsRepeater.Items.Count &gt; 0) {
          if (attachmentsRepeater.Items.Count &gt; 1) {
              returnValue += "&lt;li&gt;";
          }
          returnValue += "&lt;a href=\"" + DataBinder.Eval(Container.DataItem, "location");
          if (DataBinder.Eval(Container.DataItem, "location").ToString().EndsWith("/")) {
              returnValue += DataBinder.Eval(Container.DataItem, "name");
          }
          returnValue += "&gt;" + DataBinder.Eval(Container.DataItem, "name") + "&lt;/a&gt;";
          if (attachmentsRepeater.Items.Count &gt; 1) {
              returnValue += "&lt;/li&gt;";
          }
      }
      return returnValue;
  }
  </code></pre>
  
  <p>The code that is output is</p>
  
  <pre><code>&lt;h3&gt;Attachment&lt;/h3&gt;
  &lt;p&gt; &lt;/p&gt;
  </code></pre>
  
  <p>From this output I know that Item.Count == 1 since there is output, the H3 is singular and there is a P tag. If Item.Count > 1, H3 would be plural and there would be a UL tag.</p>
  
  <p>Is this codebehind method being run before the data is bound? Any workarounds for this? Thanks for your help.</p>
  
  <p>This was working for me before, but I had to change it to fulfill a new requirement, which is when it stopped working.</p>