|
| 1 | +--- |
| 2 | +title: Confirm Delete command with AutoGenerateDeleteColumn in RadGrid |
| 3 | +description: Check out how to Confirm Delete command with AutoGenerateDeleteColumn in RadGrid with window.confirm() or radConfirm() |
| 4 | +type: how-to |
| 5 | +page_title: Confirm Delete command with AutoGenerateDeleteColumn in RadGrid |
| 6 | +slug: grid-confirm-delete-command-autogenerateddeletecolumn |
| 7 | +ticketid: 1613630 |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Description |
| 12 | + |
| 13 | +How to prompt the user to confirm deletion of a `GridItem` when the delete column is automatically generated via enabling the `AutoGenerateDeleteColumn` property of RadGrid? |
| 14 | + |
| 15 | + |
| 16 | +## Solution |
| 17 | + |
| 18 | +Multiple ways to implement confirmation for deleting an item in RadGrid are described in the [Adding a Delete Confirmation]({%slug grid/data-editing/delete-records/adding-a-delete-confirmation%}) article. |
| 19 | + |
| 20 | +When it comes to using the auto-generated delete column of RadGrid the most convenient approach is to handle and conditionally cancel the client-side [OnCommand]({%slug grid/client-side-programming/events/oncommand%}) event of the Grid. |
| 21 | + |
| 22 | +Here is a sample approach how to confirm the deletion with window.confirm() or [RadWindowManager's RadConfirm Dialog]({%slug indow/alert,-confirm,-prompt-dialogs/radconfirm-dialog%}) functionality: |
| 23 | + |
| 24 | +````ASPX |
| 25 | +<script> |
| 26 | + var preventCommand = true; |
| 27 | +
|
| 28 | + function gridCommand(sender, args) { |
| 29 | + var commandName = args.get_commandName(); |
| 30 | +
|
| 31 | + if (commandName == 'Delete') { |
| 32 | + var itemIndex = args.get_commandArgument(); |
| 33 | + var tableView = args.get_tableView(); |
| 34 | +
|
| 35 | + //make sure that the field name is added to the ClientDataKeyNames collection |
| 36 | + var orderId = tableView.get_dataItems()[itemIndex].getDataKeyValue("OrderID"); |
| 37 | +
|
| 38 | + var confirmMessage = "Are you sure you want to delete item with ID " + orderId + "?"; |
| 39 | +
|
| 40 | + ////use window.confirm() |
| 41 | + //args.set_cancel(!confirm(confirmMessage)); |
| 42 | +
|
| 43 | + //use radconfirm() |
| 44 | + if (preventCommand) { |
| 45 | + args.set_cancel(preventCommand); |
| 46 | + radconfirm(confirmMessage, confirmCallBackFn, 300, 200, null, "Confirm"); |
| 47 | + } |
| 48 | +
|
| 49 | + function confirmCallBackFn(arg) { |
| 50 | + if (arg) { |
| 51 | + preventCommand = false; |
| 52 | + tableView.fireCommand(commandName, itemIndex); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +</script> |
| 58 | +
|
| 59 | +<telerik:RadWindowManager runat="server" ID="RadWindowManager1"></telerik:RadWindowManager> |
| 60 | +
|
| 61 | +<asp:Label Text="Log:" runat="server" /><br /> |
| 62 | +<asp:Label Text="" ID="Label1" runat="server" /> |
| 63 | +
|
| 64 | +<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" |
| 65 | + AutoGenerateEditColumn="true" |
| 66 | + AutoGenerateDeleteColumn="true" |
| 67 | + DataSourceID="SqlDataSource1" |
| 68 | + AllowAutomaticInserts="true" |
| 69 | + AllowAutomaticUpdates="true" |
| 70 | + AllowAutomaticDeletes="true" |
| 71 | + OnDeleteCommand="RadGrid1_DeleteCommand" |
| 72 | + OnItemCommand="RadGrid1_ItemCommand"> |
| 73 | + <ClientSettings> |
| 74 | + <ClientEvents OnCommand="gridCommand" /> |
| 75 | + </ClientSettings> |
| 76 | + <MasterTableView DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CommandItemDisplay="Top" |
| 77 | + DataKeyNames="OrderID" ClientDataKeyNames="OrderID"> |
| 78 | + <Columns> |
| 79 | + <telerik:GridNumericColumn DataField="OrderID" DataType="System.Int32" |
| 80 | + FilterControlAltText="Filter OrderID column" HeaderText="OrderID" |
| 81 | + ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID"> |
| 82 | + </telerik:GridNumericColumn> |
| 83 | + <telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime" |
| 84 | + FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate" |
| 85 | + SortExpression="OrderDate" UniqueName="OrderDate"> |
| 86 | + </telerik:GridDateTimeColumn> |
| 87 | + <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal" |
| 88 | + FilterControlAltText="Filter Freight column" HeaderText="Freight" |
| 89 | + SortExpression="Freight" UniqueName="Freight"> |
| 90 | + </telerik:GridNumericColumn> |
| 91 | + <telerik:GridBoundColumn DataField="ShipName" |
| 92 | + FilterControlAltText="Filter ShipName column" HeaderText="ShipName" |
| 93 | + SortExpression="ShipName" UniqueName="ShipName"> |
| 94 | + </telerik:GridBoundColumn> |
| 95 | + <telerik:GridBoundColumn DataField="ShipCountry" |
| 96 | + FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry" |
| 97 | + SortExpression="ShipCountry" UniqueName="ShipCountry"> |
| 98 | + </telerik:GridBoundColumn> |
| 99 | + </Columns> |
| 100 | + </MasterTableView> |
| 101 | +</telerik:RadGrid> |
| 102 | +
|
| 103 | +<asp:SqlDataSource ID="SqlDataSource1" runat="server" |
| 104 | + ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| 105 | + InsertCommand="INSERT INTO [Orders] ([OrderDate], [Freight], [ShipName], [ShipCountry]) VALUES (@OrderDate, @Freight, @ShipName, @ShipCountry)" |
| 106 | + SelectCommand="SELECT [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]" |
| 107 | + UpdateCommand="UPDATE [Orders] SET [OrderDate] = @OrderDate, [Freight] = @Freight, [ShipName] = @ShipName, [ShipCountry] = @ShipCountry WHERE [OrderID] = @OrderID" |
| 108 | + DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID"> |
| 109 | + <InsertParameters> |
| 110 | + <asp:Parameter Name="OrderDate" DbType="DateTime" /> |
| 111 | + <asp:Parameter Name="Freight" DbType="Decimal" /> |
| 112 | + <asp:Parameter Name="ShipName" DbType="String" /> |
| 113 | + <asp:Parameter Name="ShipCountry" DbType="String" /> |
| 114 | + </InsertParameters> |
| 115 | + <UpdateParameters> |
| 116 | + <asp:Parameter Name="OrderID" DbType="Int32" /> |
| 117 | + <asp:Parameter Name="OrderDate" DbType="DateTime" /> |
| 118 | + <asp:Parameter Name="Freight" DbType="Decimal" /> |
| 119 | + <asp:Parameter Name="ShipName" DbType="String" /> |
| 120 | + <asp:Parameter Name="ShipCountry" DbType="String" /> |
| 121 | + </UpdateParameters> |
| 122 | + <DeleteParameters> |
| 123 | + <asp:Parameter Name="OrderID" DbType="Int32" /> |
| 124 | + </DeleteParameters> |
| 125 | +</asp:SqlDataSource> |
| 126 | +```` |
| 127 | + |
| 128 | +````C# |
| 129 | +protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e) |
| 130 | +{ |
| 131 | + Label1.Text += "DeleteCommand event Fired!<br />"; |
| 132 | +} |
| 133 | + |
| 134 | +protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) |
| 135 | +{ |
| 136 | + Label1.Text += "ItemCommand event Fired! for Command: "+e.CommandName+"<br />"; |
| 137 | +} |
| 138 | +```` |
| 139 | + |
| 140 | + |
| 141 | + |
0 commit comments