简体   繁体   中英

imagebutton onclientclick and onclick events

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   
}

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>

I think the line you have written:

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

Should be replaced with:

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

as no need of alert for confirm box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM