1

如果我ImageButton动态创建并将其添加到表格单元格中

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

点击事件不起作用。

但是,如果我使用Panel而不是 tableimagebutton作品。Morevoer 如果我page直接将图像按钮添加到它也可以。但我需要存储imagebuttontable cell.

什么是问题?如何让工作imagebutton在里面asp table

点击的方法是:

protected void click(object sender, EventArgs e)
{
      Response.Write("<script type='text/javascript'>alert('OK!');</script>"); 
}
4

1 回答 1

1

什么是问题?

这似乎是一个经典的asp.net 页面生命周期问题。原因可能是,在应该处理单击的时间,您的按钮以及您的表格不存在,因为您在页面生命周期的后期创建它(例如在 pre_render 中),或者您只在未打开时创建回发,或在另一个控件事件处理程序中。

如何在asp表中制作工作图像按钮?

您的表格和按钮创建以及单击处理程序绑定应位于

page_load(更好的是,在 Init 中,当使用 view_state 进行动态控件时)

无论是否在回发,代码都应该运行。

希望这会有所帮助

于 2012-10-08T11:43:20.763 回答