动态创建ImageButton点击事件 [英] Dynamically cretaed ImageButton click event

查看:181
本文介绍了动态创建ImageButton点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我动态创建了多个HtmlTableCells,其中添加了图像按钮。当我单击一个ImageButton时,我想知道哪一个然后我可以在ImageClickEventHandler中添加代码。如果您知道如何识别单击的图像按钮(或选择的项目),请分享您的体验。提前致谢。

In my project, I dynamically created multiple HtmlTableCells in which image buttons are added. When I click an ImageButton, I want to know which one and then I can add the code in the ImageClickEventHandler. If you know how to identify the imageButton clicked (or the item seleted), please share your experience. Thanks in advance.

for (int i = 0; i < dt.Rows.Count; i++) {
            System.Web.UI.WebControls.ImageButton imageBtn = new System.Web.UI.WebControls.ImageButton();
            imageBtn.ImageUrl = dt.Rows[i]["ImagePath"].ToString();
            imageBtn.CommandArgument = i.ToString();
            //imageBtn.Command += new CommandEventHandler(ImageBtnClick);
            imageBtn.Click += new ImageClickEventHandler(imageBtn_Click);
            placeholder1.Controls.Add(imageBtn);
            HtmlTableCell imageCell2 = new HtmlTableCell();
            imageCell2.Controls.Add(imageBtn);
            row.Cells.Add(imageCell2);
}



......


......

void imageBtn_Click(object sender, ImageClickEventArgs e) {
    // Add code here
}

推荐答案

添加一个 ID ImageButton

Add one ID to ImageButton.
imageBtn.ID = "ImageButton" + i;



查看 ID 内部点击事件并从您可以知道的事件处理程序代码中执行必要的操作。


Check this ID inside click event and do necessary operation.






from your event handler code you can know.

 void imageBtn_Click(object sender, ImageClickEventArgs e) 
{
    // Add code here
    ImageButton sourceImageButton = sender as ImageButton;

   if  (sourceImageButton.ID == "YourButtonId")
   {
     //You know which image button fire that event
   }
   //...
}


这篇关于动态创建ImageButton点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆