[英]how to use an ImageButton.OnClick if the ImageButton was created in code behind
I created an ImageButton in code behind. 我在后面的代码中创建了一个ImageButton。 Now I need that when a client press the ImageButton, the ImageButton will do a certain function.
现在我需要当客户端按下ImageButton时,ImageButton将执行某个功能。 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: 声明要在onClick事件上触发的回调:
public void DynamicClick(object sender, EventArgs e) {
// do something
}
Add it to the Click
EventHandler
list: 将其添加到
Click
EventHandler
列表:
img.Click += new EventHandler(DynamicClick);
See also: Setting LinkButton's OnClick event to method in codebehind . 另请参阅: 将LinkButton的OnClick事件设置为代码隐藏中的方法 。
EventHandler does not work with LinkButton. EventHandler不能与LinkButton一起使用。 Try the below:
试试以下内容:
img.Click += new ImageClickEventHandler(onImageClick);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.