1

I've a repeater on my page. And it's containing a nested repeater on it's item. It's structure is as below

<asp:Repeater ID="rptOuterRepeater" runat="server" >
   <ItemTemplate>
            <tr >
              <td>
              // Need some code logic here for counting
              </td>
           </tr>
            <tr>
            <td>
                 <asp:Repeater ID="rptInnerRepeater" unat="server">
                <ItemTemplate>
                     <tr >
                        <td>
                          &nbsp;
                       </td>
                     </tr>
                  </ItemTemplate>
             </td>
            </tr>
     </ItemTemplate>
   </asp:Repeater>

I need to count rptInnerRepeater 's item count in place of comments. Is this possible using Inline Code.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
  • What datatype is the datasource for the repeater? – Kao Mar 21 '13 at 08:03
  • Its `IList`...But i don't think there will be any use of Datasource while counting Inline subitems.. – Mayank Pathak Mar 21 '13 at 08:04
  • I'm guessing you repeater repeats the IList, and the subRepeater, repeats something stored in the child of the current IList item, otherwise I think I need a bit more explaining of the purpose of the subRepeater. – Kao Mar 21 '13 at 08:10
  • Yes it is possible, but you need to show us where/how you are binding the inner repeater. – Shai Cohen Mar 21 '13 at 15:31

2 Answers2

0

Since, you are binding your Repeater to a Datasource, Why cannot you manipulate your datasource to contain the Count of the SubItems Collection to which your inner repeater is bound?

You can do it this way:

<asp:Repeater ID="rptOuterRepeater" runat="server">
  <ItemTemplate>
      <table>
         <tr>
           <td>
              <%# Eval("Count") %>
           </td>
         </tr>
         <tr>
           <td>
              <asp:Repeater ID="rptInnerRepeater" runat="server" 
                      DataSource='<%# Eval("Items") %>'>
               <ItemTemplate>
               <tr>
                 <td>
                    <%# Eval("Id") %>
                </td>
               </tr>
       </ItemTemplate>
    </asp:Repeater>
   </td>
 </tr>
   </table>
</ItemTemplate>
</asp:Repeater>

and your DataSource is :

  var datasource = testList.Select(s =>
                                 new
                                 {
                                     Count = s.Items.Count,
                                     Items = s.Items,
                                     Id = s.Id
                                 }).ToList();
            rptOuterRepeater.DataSource = datasource;
            rptOuterRepeater.DataBind();

so why not modify your datasource collection to contain an extra field called Count?

on the client side, you will have to use very ugly javascript/jQuery code, which will be superbly specific to your need.

Manish Mishra
  • 12,163
  • 5
  • 35
  • 59
0

Best I could come with in terms of short coding, using a literal and a ItemDataBound event handler. Not really what I would call "inline", but I hope this will help anyway.

<script runat="server">
    void innerRpItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.DataItem != null)
        {
            var lit = (Literal) ((Control) sender).Parent.FindControl("count");
            lit.Text = string.IsNullOrWhiteSpace(lit.Text) ? "1" : (int.Parse(lit.Text) + 1).ToString();
        }
    } 
</script>

<asp:Repeater ID="rptOuterRepeater" runat="server" >
   <ItemTemplate>
            <tr >
              <td>
              // Need some code logic here for counting
              <asp:Literal runat="server" ID="count"/>
              </td>
           </tr>
            <tr>
            <td>
                 <asp:Repeater ID="rptInnerRepeater" runat="server" OnItemDataBound="innerRpItemDataBound" >
                <ItemTemplate>
                     <tr >
                        <td>
                          &nbsp;
                       </td>
                     </tr>
                  </ItemTemplate>
             </td>
            </tr>
     </ItemTemplate>
   </asp:Repeater>

---------------- previous non working solution

You may try to replace this line

// Need some code logic here for counting

with this one

<%#((Repeater)Container.FindControl("rptInnerRepeater")).Items.Count%>

Not tested, and not really sure it would work, but worth a try I would say :-)

Hope this will help

jbl
  • 15,179
  • 3
  • 34
  • 101