views:

365

answers:

1

I have a form that searches all rows in a single table (TServices) that occurred between the date range and then populates a dynamic table below the form.

Now I'm adding a delete ImageButton next to each listing in the table. Here's my C# code-behind for adding the ImageButtons to the table cell:

        ImageButton btnRemoveService = new ImageButton();
        btnRemoveService.EnableViewState = true;
        btnRemoveService.ImageUrl = "/images/icons/trash.gif";
        btnRemoveService.Click += new ImageClickEventHandler(btnRemoveService_Click);
        btnRemoveService.ID = "btnRemoveService-" + row["BillingID"].ToString();
        btnRemoveService.CommandArgument = row["BillingID"].ToString();
        tblCell.Controls.Add(btnRemoveService);

Even if I put nothing in btnRemoveService_Click(), all my data from the dynamic table disappears. When I put in the code to actually delete the stored procedure, the data disappears from the dynamic table and nothing is changed in the db.

Edit: I moved the populate code into its own function and called it at the end of btnRemoveService_Click(). Shouldn't this reload the data?

+1  A: 

Your error is due to the fact that you must add dynamically created controls to the page on EVERY load. If you re-add them inside the page_init method they will exist for viewstate to load and for the click events to fire successfully. It is all related to the ASP.NET Page Load process

Mitchel Sellers