简体   繁体   中英

how to use an ImageButton.OnClick if the ImageButton was created in code behind

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

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 .

EventHandler does not work with LinkButton. Try the below:

img.Click += new ImageClickEventHandler(onImageClick);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM