3

I created an ImageButton in code behind. Now I need that when a client press the ImageButton, the ImageButton will do a certain function. The problem is that it doesn't do this certain function.

this is the code-

var img = new ImageButton();
img.ID = "Del" + i.ToString();
img.ImageUrl = "images/1395958363_meanicons_57.png";
img.Width = 48;
img.Height = 38;
img.Click += new System.Web.UI.ImageClickEventHandler(this.btnForDelete_Click); //doesn't work!!

Lab.Controls.Add(img); //put the image in a label
user3475785
  • 191
  • 2
  • 3
  • 7

2 Answers2

2

Declare a callback you want to be triggered upon onClick event:

public void DynamicClick(object sender, EventArgs e) {
    // do something
}

Add it to the Click EventHandler list:

img.Click += new EventHandler(DynamicClick);

See also: Setting LinkButton's OnClick event to method in codebehind.

Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245
2

EventHandler does not work with LinkButton. Try the below:

img.Click += new ImageClickEventHandler(onImageClick);
Rizier123
  • 58,877
  • 16
  • 101
  • 156
user1630359
  • 103
  • 8