imagebutton onclientclick和onclick事件

时间:2013-08-15 06:54:37

标签: asp.net events onclick onclientclick

如何将代码后面的onclentclick和onclick事件添加到图像按钮。我写了一些代码,它显示了onclientclick事件的确认消息,但没有触发onclick事件。

我的代码如下......

ImageButton img = new ImageButton();
img.ID = "img1";
img.Visible = true;
img.OnClientClick = "alert(confirm('Are you sure you want to delete folder with files ?'));";
img.Click += new ImageClickEventHandler(img_Click);
img.ImageUrl = "~/images/admin/remove.png";

活动的代码是:

void img_Click(object sender, ImageClickEventArgs e)
{
 //code what i want to implement   
}

2 个答案:

答案 0 :(得分:1)

如下所示更改OnClientClick

   img.OnClientClick = "return confirm('Are you sure you want to delete folder with files ?');";

或者您可以在aspx页面中添加脚本作为方法,然后从OnClientClick

调用该脚本
img.OnClientClick ="ConfirmDelete()";


<script type="text/javascript">
function ConfirmDelete()
  {
      return confirm('Are you sure you want to delete folder with files ?');
  }
</script>

答案 1 :(得分:0)

我认为你写的这行:

img.OnClientClick = "alert(confirm('Are you sure you want to delete folder with files ?'));";

应替换为:

img.OnClientClick = "return confirm('Are you sure you want to delete folder with files ?');";

无需警告确认框。