ASP嵌套转发器ID

时间:2017-08-30 19:43:20

标签: asp.net twitter-bootstrap

我正在使用bootstrap来折叠和扩展一个表,它工作正常但我使用的是类而不是ID。有了这个,扩展一行扩展所有行而不仅仅是那一行。我的问题是我的数据目标如何指向嵌套的转发器ID? transactionCollapse ID无法直接定位,我已尝试执行<%=transactionGroupedList.FindControl("transactionCollapse")%>,但它引发了错误。

<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>

Online Payment行是折叠/展开下面的Posting -MP Payment行的内容。此用户只有一个Online Payment,但许多用户将拥有多个。 This is the output.

1 个答案:

答案 0 :(得分:1)

你有几个问题。首先,在Repeater / GridView等中使用FindControl是基于索引的。因此,您需要在正确的项目上使用FindControl。

transactionGroupedList[i].FindControl("transactionCollapse")

但是上述操作仍然无效,因为transactionCollapse位于嵌套的Repeater中,需要首先找到,然后才能访问正确的Item Index。

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

但是这也行不通,因为FindControl不知道transactionDetailList是一个带有索引项的Repeater。因此,您需要首先投射嵌套的Repeater,然后才能访问它的项目。所以它变成了这个

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