Confirmation dialog for deleting a record in a GridView
When you want to delete a row in a GridView ASP.NET, is not normally post any confirmation. In some cases, to avoid accidental deletions of records, it is useful to bring up a confirmation dialog. If the user clicks the OK button, the record will be deleted from the database, if the user clicks Cancel no records will be deleted.
The following example shows how to insert a button in the GridView to delete the row. You use the OnClickClient event of the button, the javascript code is inserted to show a confirmation dialog and returns true if OK is pressed, false otherwise.
<asp:GridView runat="server"
ID="GridViewUsers"
AutoGenerateColumns="False"
DataKeyNames="Email"
DataSourceID="AccessDataSourceUsers">
<Columns>
<asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" SortExpression="Email" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server"
ID="ImageButtonDelete"
ImageUrl="images/delete.png"
ToolTip="Delete current row"
Text="Delete"
CommandName="Delete"
OnClientClick="return window.confirm('Do you want to delete the selected record?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSourceUsers" runat="server" ConflictDetection="CompareAllValues" DataFile="~/App_Data/DB-User.mdb"
SelectCommand="SELECT [Email], [Name], [Surname], [City], [Country] FROM [Accounts] ORDER BY [Name]"
DeleteCommand="Delete * FROM [Accounts] WHERE [Email]=?"></asp:AccessDataSource>