How to display a confirm box while deleting an item in Gridview?

Confirm delete for GridView's Link Button using JavaScript

Here is the code using Asp.net 2.0


<script type="text/javascript">

function confirm_delete()

{

if (confirm("Are you sure you want to delete the contact?")==true)

return true;

else

return false;

}

</script>





<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDeleting="GridView1_RowDeleting">

<Columns>

<asp:BoundField DataField="ID" SortExpression="ID" HeaderText="ID" />

<asp:BoundField DataField="Name" SortExpression="Name" HeaderText="Name" />

<asp:BoundField DataField="City" SortExpression="City" HeaderText="City" />

<asp:TemplateField HeaderText="Delete">

<ItemTemplate>

<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="javascript : return confirm('Do you really want to \ndelete the
item?');"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

No comments: