System.Web.UI.WebControls Namespace GridView Class
.NET Framework version 2.0
Sets or retrieves a value specifying whether a CommandField column with a Delete button for each data row is automatically added and displayed in a GridView control.
Inline |
<asp:gridview autogeneratedeletebutton = true | false ... > |
Script |
GridView.AutoGenerateDeleteButton [ = true | false ] |
This property accepts or returns only a boolean value: true if a CommandField column with a Delete button for each data row is automatically added and displayed; otherwise, false. Default value is false.
Use this property to automatically create a CommandField column with a Delete button for each data row in a GridView control.
Clicking the Delete button for a row permanently removes that record from the data source.
Note that for the automatic deletion feature to work, the GridView must be bound to a data source control that is configured to delete data, meaning the DeleteCommand must have been properly set.
In addition, the DataKeyNames property must be set to specify the key field or fields of the data source that will identify the record to delete.
The GridView control raises the following events to which you can assign a custom action to perform when a row is deleted.
Event |
Description |
RowDeleting |
Occurs when a row's Delete button is clicked, but before the GridView control deletes the record from the data source. This event is often used to cancel the deletion operation. |
RowDeleted |
Occurs when a row's Delete button is clicked, but after the GridView control deletes the record from the data source. This event is often used to check the results of the delete operation. |
The below snippet demonstrates how to use the AutoGenerateDeleteButton property to enable the automatic deletion feature of a GridView control.
<asp:gridview id = "messagesGrid" runat = "server"
datasourceid = "messages"
datakeynames = "MessageID"
autogeneratecolumns = "false"
autogeneratedeletebutton ... >
WARNING: Deleting rows in a database table CANNOT BE UNDONE, which can result in serious data loss. In a real-world scenario, only the database administrator or an authorized user must be granted DELETE permission. The examples provided here are only for learning purposes.
GridView Members AutoGenerateEditButton AutoGenerateSelectButton Allowing Users to Delete Rows in a GridView Control