2

我有这个代码:

<asp:Repeater id="repeaterCategories" runat="server">
    <ItemTemplate>
        <div class="categorie-item">
            ...

            <asp:Repeater id="repeaterSubCategories" runat="server">
                <ItemTemplate>
                    ...
                </ItemTemplate>
            </asp:Repeater>
        </div>                    
    </ItemTemplate>
</asp:Repeater>

并且repeaterSubCategories必须是repeaterCategories.SubCategories,对于每个repeaterCategories。所以我必须动态绑定(对于每个第一次转发器迭代)子类别列表。

我可以做吗?如何?

4

1 回答 1

6

如果您有这样的嵌套中继器:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <asp:Repeater ID="Repeater2" runat="server"></asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

您可以使用它来绑定它:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var data = ((MyClass)e.Item.DataItem).Subcategories;
    var repeater2 = (Repeater)e.Item.FindControl("Repeater2");
    repeater2.DataSource = data;
    repeater2.DataBind();
}
于 2013-04-23T13:28:27.323 回答