不会触发动态创建的图像按钮单击事件 [英] Dynamically created image button click event is not fired

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

问题描述

在网格RowDataBound基于某些条件我动态添加了imagebutton。并行我想为此按钮单击添加事件。这是我的代码片段

 protected void GV3_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow )
{
TableCell cell = new TableCell();
ImageButton bttn = new ImageButton();
bttn.CommandName =删除;
bttn.ImageUrl =delete.png;
bttn.ToolTip =点击删除此记录;
bttn.CommandArgument = e.Row.Cells [0] .Text;
cell.Controls.Add(bttn);
e.Row.Cells.Add(cell);
bttn.OnClientClick =返回确认('你确定要删除+ e.Row.Cells [0] .Text +?');;
}
}



该消息弹出框出现,但我无法捕获该点击事件。点击一行我要删除该行,所以想要动态创建事件。为此,我使用了以下动态点击事件



 bttn.Click + =  new  ImageClickEventHandler(b_Click); 

private void b_Click(对象发​​件人,EventArgs e)
{
// 删除记录
}



但上面这段代码也行不通。



请帮帮我解决这个。在此先感谢。

解决方案

如下所示...

 ImageButton bttn =  new  ImageButton(); 
bttn.CommandName = 删除;
bttn.ImageUrl = delete.png;
bttn.ToolTip = 点击删除此记录;
bttn.CommandArgument = e.Row.Cells [ 0 ]。文字;

bttn.OnClientClick = 返回确认('你确定要删除 + e.Row.Cells [ 0 ]。文本+ ');?;
bttn.Click + = new ImageClickEventHandler(b_Click);

cell.Controls.Add(bttn);
e.Row.Cells.Add(cell);



现在,活动看起来像......

  void  b_Click( object  sender,ImageClickEventArgs e)
{
// 做你想做的事。
}


On grid RowDataBound based on some condition i've added imagebutton dynamically. parallelly i want to add event for this button click. here is my code snippet

protected void GV3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell cell = new TableCell();
                ImageButton bttn = new ImageButton();
                bttn.CommandName = "Delete";
                bttn.ImageUrl = "delete.png";
                bttn.ToolTip = "Click to delete this record";
                bttn.CommandArgument = e.Row.Cells[0].Text;
                cell.Controls.Add(bttn);
                e.Row.Cells.Add(cell);
                bttn.OnClientClick = "return confirm('Are you sure you to delete " + e.Row.Cells[0].Text + "?');";
}
}


That message popup box comes, but i'm unable to catch that click event. while click on a row i want to delete that row, so want to create event dynamically. for that i've used this following dynamic click event

bttn.Click += new ImageClickEventHandler(b_Click);

private void b_Click(object sender, EventArgs e)
    {
//delete record
}


but this above code also not working.

Please help me to solve this. Thanks in advance.

解决方案

Do like below...

ImageButton bttn = new ImageButton();
bttn.CommandName = "Delete";
bttn.ImageUrl = "delete.png";
bttn.ToolTip = "Click to delete this record";
bttn.CommandArgument = e.Row.Cells[0].Text;

bttn.OnClientClick = "return confirm('Are you sure you to delete " + e.Row.Cells[0].Text + "?');";
bttn.Click += new ImageClickEventHandler(b_Click);

cell.Controls.Add(bttn);
e.Row.Cells.Add(cell);


Now, event would look like...

void b_Click(object sender, ImageClickEventArgs e)
{
    // Do whatever you want to do.
}


这篇关于不会触发动态创建的图像按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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