Reputation: 91
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
}
Upvotes: 4
Views: 16853
Reputation: 6938
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.
Upvotes: 0
Reputation: 63105
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>
Upvotes: 1