If I create ImageButton dynamically and add this into table cell
TableRow tr = new TableRow();
TableCell tc = new TableCell();
ImageButton imagebutton= new ImageButton();
imagebutton.imageurl=imageurl;
imagebutton.click+= new System.Web.UI.ImageClickEventHandler(click);
tc.Controls.Add(imagebutton);
tr.Controls.Add(tc);
Table1.Rows.Add(tr);
The click event doesnt’ work.
However, if I use Panel instead of table the imagebutton works. Morevoer If I add imagebutton to the page directly it also works.
But I need store imagebutton in a table cell.
What is a problem?
How to make working imagebutton inside asp table?
The method click is:
protected void click(object sender, EventArgs e)
{
Response.Write("<script type='text/javascript'>alert('OK!');</script>");
}
This seems to be a classic asp.net page life-cycle problem. It’s cause is probably that, a the time the click should be handled, your button, as well as your table, do not exist because you create it later in the page life cycle (say in pre_render), or you only create when not on postback, or in another control event handler.
Your table and button creation , as well as click handler binding should be located in
page_load (even better, in Init, when using view_state for dynamic controls)
The code should run whether on postback or not.
Hope this will help