Zamereon
Zamereon

Reputation: 376

ASP Nested Repeater IDs

I'm using bootstrap to collapse and expand a table, which is working fine but I'm using classes instead of IDs. With this, expanding one row expands all the rows rather than just that one. My question is how does my data-target point at a nested repeater id? The transactionCollapse ID is unable to be targeted directly and I've tried doing <%=transactionGroupedList.FindControl("transactionCollapse")%> but it threw an error.

<tbody>
    <asp:Repeater runat="server" ID="transactionGroupedList" OnItemDataBound="TransactionGroupedDataList_ItemDataBound">
        <ItemTemplate>
            <tr>
                <!-- This line should target the transactionCollapse ID below instead of the class -->
                <td data-toggle="collapse" data-target=".transactionCollapse">
                    <span id="transactionGroupCollapseIcon" runat="server" class="fonticon-down-arrow"></span>
                    <custom:Label runat="server" ID="transactionActivityDataColumnLabel"></custom:Label>&nbsp;
                </td>
                <td>
                    <custom:Label runat="server" ID="transactionDateDataColumnLabel">
                    </custom:Label>
                </td>
                <td>
                    <custom:Label runat="server" ID="transactionNumberDataColumnLabel">
                    </custom:Label>
                </td>
                <td>
                    <custom:Label runat="server" ID="transactionAmountDataColumnLabel">
                    </custom:Label>
                </td>
                <td>
                    <custom:Label runat="server" ID="transactionStatusDataColumnLabel">
                    </custom:Label>
                </td>
            </tr>
            <asp:Repeater runat="server" ID="transactionDetailList" OnItemDataBound="TransactionDetailsDataList_ItemDataBound">
                <ItemTemplate>
                    <tr id="transactionCollapse" runat="server" class="collapse transactionCollapse">
                        <td colspan="2">
                            <custom:Label runat="server" ID="transactionDetail">
                            </custom:Label>
                        </td>
                        <td>
                            <custom:Label runat="server" ID="transactionDetailTransactionNumber">
                            </custom:Label>
                        </td>
                        <td>
                            <custom:Label runat="server" ID="transactionDetailAmount">
                            </custom:Label>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>
</tbody>

The Online Payment row is what collapses/expands the Posting - and MP Payment rows below. This user only has one Online Payment, but many users will have multiple. This is the output.

Upvotes: 1

Views: 108

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You have a couple of problems. First of all when using FindControl inside a Repeater/GridView etc is index based. So you need to use FindControl on the correct Item.

transactionGroupedList[i].FindControl("transactionCollapse")

However the above will still not work because transactionCollapse is in a nested Repeater that needs to be found first and then access the correct Item Index.

transactionGroupedList.Items[0].FindControl("transactionDetailList").Items[0]...

But this will also not work since FindControl does not know that transactionDetailList is a Repeater with index based Items. So you need to cast the nested Repeater first before you can access it's items. So it becomes this

<%= ((Repeater)transactionGroupedList.Items[i].FindControl("transactionDetailList")).Items[i].FindControl("transactionCollapse").ClientID %>

Upvotes: 1

Related Questions