简体   繁体   English

如果ImageButton是在后面的代码中创建的,如何使用ImageButton.OnClick

[英]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 . 另请参阅: 将LinkBut​​ton的OnClick事件设置为代码隐藏中的方法

EventHandler does not work with LinkButton. EventHandler不能与LinkBut​​ton一起使用。 Try the below: 试试以下内容:

img.Click += new ImageClickEventHandler(onImageClick);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ImageButton OnClick后面的代码中实现if else条件 - How to implement an if else condition in the code behind for ImageButton OnClick 如果ImageButton是在后面的代码中创建的,如何获取javascript的ImageButton的ID? - How can I get the ID of ImageButton for javascript, if the ImageButton was created in code behind? 如何在C#中的代码中使用ImageButton - How to work with ImageButton in code behind in C# 从ImageButton传递参数到后面的代码 - passing a parameter from a ImageButton to code behind 在gridview中使用imagebutton在代码后面调用functon - Calling a functon in code behind by using an imagebutton in a gridview 当ImageButton具有onClick事件处理程序时,如何在客户端禁用.NET ImageButton控件? - How can you disable a.NET ImageButton control on the client side when the ImageButton has an onClick event handler? 使用代码隐藏控制ImageButton中的名称属性 - Control the Name Attribute within an ImageButton using code-behind 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-> Itemtemplate->面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file 如何在C#中创建事件时将事件添加到ImageButton? - How to add an event to a ImageButton when is created on C#? 如何使ImageButton无法点击 - How to make ImageButton not clickable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM