imagebutton onclientclick and onclick events

baby picture baby · Aug 15, 2013 · Viewed 15k times · Source

how to add onclentclick and onclick events from code behind to image button. I wrote some code it shows confirm message for onclientclick event but not firing onclick event.

my code is as following..

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";

Event's code is :

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

Answer

Damith picture Damith · Aug 15, 2013

change OnClientClick as bellow

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

Or you can add script in your aspx page as method and then call that from OnClientClick

img.OnClientClick ="ConfirmDelete()";


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