不会触发动态创建的图像按钮单击事件

编程入门 行业动态 更新时间:2024-10-19 12:41:17
本文介绍了不会触发动态创建的图像按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在网格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. }

更多推荐

不会触发动态创建的图像按钮单击事件

本文发布于:2023-11-02 14:26:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552658.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   图像   事件   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!