1

This is HTML Code:

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

This is Code behind:

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 event may be can't find itself Repeater (Repeater2).

M. Rashford
  • 121
  • 1
  • 12
  • `Repeater2_ItemCommand` is not attached to Repeater2 in the aspx code. Did you debug the code ? Are you able to hit the break point in `Repeater2_ItemCommand`? – Chetan Aug 02 '17 at 03:16
  • And not sure what is the sense of the logic of event handler. You are trying to get items of repeater as ArrayList then adding count to that list and then binding that ArrayList to repeater again and that too on click of delete button. What am I missing here? – Chetan Aug 02 '17 at 03:25
  • It break in line: var list = new ArrayList(Repeater2.Items); – M. Rashford Aug 02 '17 at 03:51
  • What error you getting? Did you understand my first comment? Can you explain the logic of event handler? – Chetan Aug 02 '17 at 03:54
  • 'Object reference not set to an instance of an object.' When I press button in child Repeater (delete row of pressed button), then i bind child Repeater data. – M. Rashford Aug 02 '17 at 04:16

1 Answers1

2

You can't use FindControl method. In this cases, you should cast the source parameter to Repeater like this:

var Repeater2 = (Repeater)source;

Then you have access to all attributes of the repeater which it's item_command event is fired.

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
Reza Amini
  • 476
  • 1
  • 5
  • 20