This is a page lifecycle problem.
You're adding the ImageButton to the page (and registering your handler) during the PreRender phase, which occurs after control events have been dispatched. Therefore, your handler will never be called.
Try adding the button during the Load phase, if at all possible.
protected void Page_Init(object sender, EventArgs e)
{
ImageButton imgButton;
imgButton = new ImageButton();
imgButton.ID = "prgcode";
imgButton.ImageUrl = "~/images/2.jpg";
imgButton.ToolTip = "test";
imgButton.Click += new ImageClickEventHandler(imgButton_click);
Page.Form.Controls.Add(imgButton);
}
private void imgButton_click(object sender, ImageClickEventArgs e)
{
Response.Write("adfa");
}
Please check the following link:
http://www.aspsnippets.com/Articles/Creating-Dynamic-Button-LinkButton-and-ImageButton-in-ASP.Net.aspx[
^]
Hope this may help.