2

I have a table with data from an SQL database and the table exists of a few rows and columns. The user has to provide credentials first before the table with the data is shown so i create the table (and its contents) dynamically.

On each row in the table i add a cell with a "remove"-button in it:

   // more code to create the table above
   Button remove = new Button();
   remove.Text = "Remove";
   remove.Click += new EventHandler(remove_Click);

   TableCell last = new TableCell();
   last.Controls.Add(remove);

   row.Cells.Add(last);
   //...

When the user clicks the button i want the corresponding record in the database to be removed and the table updated after the postback.

The code for this is written in remove_Click but the event is never fired, just because the remove button doesn't exist anymore after the postback and thus the event of the button can't be fired. As explained here: Dynamically Added Event Handler Not Firing

The code works fine if i don't remove the button, but how would i go about firing the event and still wanting to remove the button ?

Community
  • 1
  • 1
Aerus
  • 4,332
  • 5
  • 43
  • 62
  • 3
    How is that even possible? The event must be fired for it to be removed, so removal before firing is probably not the issue. – Ry- Mar 21 '12 at 15:42
  • Are you sure you must remove the button and not just disable or hide it? – Roy Dictus Mar 21 '12 at 15:43
  • According to the thread you reference, you need the button to fire the event.. maybe you need to add the code somewhere else and know what type of postback you receive? – gbianchi Mar 21 '12 at 15:49
  • @minitech Agreed, i cheated a bit with the code, on postback i would remove the button anyway and the event being fired would be a text change on a label but the event to change the label never fired. @ Roy that may be the easiest solution, i'll look into that. – Aerus Mar 21 '12 at 16:04

2 Answers2

1

You have to assign a unique ID to your Button control.

code4life
  • 15,655
  • 7
  • 50
  • 82
  • I don't understand how adding a unique ID to my `Button` changes anything? I have tried this change in my project aswell as in a small example and the event still doesn't fire. – Aerus Mar 21 '12 at 16:00
  • @Aerus ASP .Net relies on this. In this way identifies which button was clicked. – Adrian Iftode Mar 21 '12 at 16:12
1

Call whatever function that builds your table AGAIN in the remove_Click event handler. Since the record doesn't exist in the database after the remove button fires, the 2nd time the BuildTable() is called, that record will not be included when the table is built.

Assuming your .aspx looks something like:

...
<asp:PlaceHolder id="phTable" runat="server">
...

Try something like this:

    PageLoad(...)
    {
        BuildTable();
    }
    BuildTable()
    {
        phTable.Controls.Clear();
        Table T = new Table();
         //do stuff
        phTable.Controls.Add(T);


    }
    protected void remove_Click(object sender, EventArgs e)
    {
        //remove record from database
        BuildTable();

    }
Robert
  • 265
  • 4
  • 14
  • Exactly what i was looking for, i felt there was a way to do this without hiding/disabling the button but couldn't figure out how exactly to do it. Thanks – Aerus Mar 21 '12 at 16:08