When working with databases and the SQL DeleteCommand it may be a good idea to double check that this is what the user intended to do before risking permanently losing data. This is normally in the form of a pop up confirmation message with options of OK or Cancel.
It’s very easy to implement. Here’s how it’s done. This example has a delete button in a GridView control.
<asp:GridView ID=”grdContactNames” runat=”server” DataSourceID=”srcContacts” AutoGenerateColumns=”false” DataKeyNames=”ID”>
<Columns>
<asp:BoundField DataField=”ID” HeaderText=”ID” />
<asp:BoundField DataField=”FirstName” HeaderText=”First Name” />
<asp:BoundField DataField=”LastName” HeaderText=”Last Name” />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID=”btnDelete” runat=”server” Text=”Delete” CommandName=”Delete” OnClientClick=”return confirm(‘Are you sure you want to delete this contact? Click OK to delete, Cancel to stop.’);” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Instead of using a ButtonField as you might expect we use a TemplateField which allows us to add the OnClientClick property. You can then add your own confirmation message. The data record will only be deleted on confirming by clicking OK. No changes to the data source control are needed.