1

这是 HTML 代码:

    <asp:Repeater ID="Repeater1" runat="server"
    OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <h1>Repeater 1</h1>
        <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
            <ItemTemplate>
                <h1>Repeater 2</h1>
               <asp:LinkButton CommandArgument='<%#Container.ItemIndex%>' CommandName="cmdDeleteItem" ID="lnkDelete" runat="server" >Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

这是背后的代码:

protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "cmdDeleteItem":
                {
                    var Repeater2= (Repeater)e.Item.FindControl("Repeater2");
                    var list = new ArrayList(Repeater2.Items);
                    list.Add(Repeater2.Items.Count);
                    Repeater2.DataSource = list;
                    Repeater2.DataBind();
                }
                break;
        }
    }

Repeater2_ItemCommand 事件可能找不到自己的Repeater (Repeater2)。

4

1 回答 1

2

你不能用FindControl方法。在这种情况下,您应该将 source 参数转换为Repeater

var Repeater2 = (Repeater)source;

item_command然后您可以访问触发它的事件的转发器的所有属性。

于 2017-08-02T05:08:35.993 回答