Asp: button click event does not fire

I am dynamically adding rows to an asp table. On each row of the table, I also include a button that has a SelectProduct_Click event.

The problem is that even if I register a click event, the event does not fire.

The button is added as follows:

btnSelect = new Button(); btnSelect.ID = "btnSelect"; btnSelect.CommandArgument = od.ProductId; btnSelect.Click += new EventHandler(this.SelectProduct_Click); btnSelect.CssClass = "button"; btnSelect.Text = "Select"; cell = new TableCell(); cell.Controls.Add(btnSelect); row.Cells.Add(cell); 

How can I launch my button on a click?

+2
source share
2 answers

You need to learn about the life cycle of an ASP.NET page .

For dynamic controls to fire their events upon postback, they need to be created again and bound to the event handler.

The best place to create (and recreate) dynamic controls is in the OnInit event handler.

+3
source

@Oded - you are absolutely right in choosing the right time to add dynamic controls. However, it is not indicated at which event he is trying to add a button.

0
source

All Articles