{"id":1939,"date":"2019-03-26T23:06:02","date_gmt":"2019-03-27T04:06:02","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=1939"},"modified":"2026-08-20T17:29:53","modified_gmt":"2026-08-20T22:29:53","slug":"how-to-add-a-delete-button-to-each-row-in-grid","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2019\/03\/26\/how-to-add-a-delete-button-to-each-row-in-grid\/","title":{"rendered":"Add a Delete Button to Each Row in a PHPRunner Grid"},"content":{"rendered":"<p>PHPRunner normally provides <strong>Delete selected<\/strong> for removing one or more records from a List page. In some applications, it is more convenient to place a Delete action directly on each row next to the standard View, Edit or Copy actions.<\/p>\n<p>This tutorial shows how to add a row-level Delete button, ask for confirmation, delete the correct record and reload the List page afterward.<\/p>\n<h2>Before you add the button<\/h2>\n<p>Place the custom button inside the grid row. This is important because the button needs access to the current record so the server-side code can determine which row should be deleted.<\/p>\n<p>You will also need to know the table name and the key field used to identify each record. In the example below, the table is <strong>categories<\/strong> and the key field is <strong>CategoryID<\/strong>. Replace those values with the corresponding names from your project.<\/p>\n<p><strong>Security note:<\/strong> a custom button that calls <strong>DB::Delete()<\/strong> does not automatically inherit all of the behavior of PHPRunner&#8217;s standard Delete action. Make sure the button is only available to users who are allowed to delete the corresponding records, and add any additional permission checks your application requires.<\/p>\n<h2>1. Add the button to the grid<\/h2>\n<p>Open the List page in Page Designer and insert a button. Move the button into one of the grid columns so it becomes part of each record row.<\/p>\n<p>Choose a button style that matches the other row actions, such as View or Edit. Select an appropriate delete or trash icon from the icons available in your current PHPRunner version.<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/03\/scr_delete_button_designer.png\" alt=\"Delete button added to a PHPRunner grid row in Page Designer\" width=\"615\" height=\"378\" class=\"alignnone size-full wp-image-1941\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/03\/scr_delete_button_designer.png 615w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/03\/scr_delete_button_designer-300x184.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/03\/scr_delete_button_designer-150x92.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/03\/scr_delete_button_designer-400x246.png 400w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/>\n<\/p>\n<p><!--more--><\/p>\n<h2>2. Ask for confirmation<\/h2>\n<p>Open the button code and add the following to the <strong>ClientBefore<\/strong> event.<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"javascript\" name=\"mshighlighter\" >\r\nif (confirm(\"Do you want to delete this record?\")) {\r\n    return true;\r\n}\r\n\r\nreturn false;<\/textarea><\/pre>\n<\/div>\n<p>Returning <strong>false<\/strong> prevents the server-side part of the button from running when the user cancels the confirmation.<\/p>\n<h2>3. Delete the current record<\/h2>\n<p>In the Server event, use <strong>getCurrentRecord()<\/strong> to get the record associated with the row where the button was clicked.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$record = $button->getCurrentRecord();\r\n\r\n$data = array();\r\n$data[\"CategoryID\"] = $record[\"CategoryID\"];\r\n\r\nDB::Delete(\"categories\", $data);<\/textarea><\/pre>\n<\/div>\n<p>The <strong>$record<\/strong> array contains the current row data. We copy the key value into <strong>$data<\/strong> and pass it to <strong>DB::Delete()<\/strong>.<\/p>\n<p>Replace <strong>categories<\/strong> with your table name and <strong>CategoryID<\/strong> with your actual key field.<\/p>\n<p>If your table uses a composite key, include all key fields in the array:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\n$record = $button->getCurrentRecord();\r\n\r\n$data = array();\r\n$data[\"OrderID\"] = $record[\"OrderID\"];\r\n$data[\"ProductID\"] = $record[\"ProductID\"];\r\n\r\nDB::Delete(\"order_details\", $data);<\/textarea><\/pre>\n<\/div>\n<p><strong>C#<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nXVar record = button.getCurrentRecord();\r\n\r\ndynamic data = XVar.Array();\r\ndata[\"CategoryID\"] = record[\"CategoryID\"];\r\n\r\nDB.Delete(\"categories\", data);<\/textarea><\/pre>\n<\/div>\n<h2>4. Reload the List page<\/h2>\n<p>After the server-side delete finishes, reload the page so the removed record disappears from the grid.<\/p>\n<p>Add the following code to the <strong>Client After<\/strong> event:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"javascript\" name=\"mshighlighter\" >\r\nlocation.reload();<\/textarea><\/pre>\n<\/div>\n<h2>How the button works<\/h2>\n<p>The complete flow is simple: the user clicks the Delete button, confirms the action, the button reads the current record, the server deletes that record using its key value, and the browser reloads the List page.<\/p>\n<p>Using <strong>getCurrentRecord()<\/strong> is what makes this technique suitable for a button placed inside a grid row. The same button code runs for every row, but PHPRunner provides the data for the specific record where the user clicked.<\/p>\n<p>For straightforward applications, this produces a convenient row-level Delete action without requiring users to select a checkbox first and then use <strong>Delete selected<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHPRunner normally provides Delete selected for removing one or more records from a List page. In some applications, it is more convenient to place a Delete action directly on each row next to the standard View, Edit or Copy actions. This tutorial shows how to add a row-level Delete button, ask for confirmation, delete the correct record and reload the List page afterward. Before you add the button Place the custom button inside the grid row. This is important because the button needs access to&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2019\/03\/26\/how-to-add-a-delete-button-to-each-row-in-grid\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Add a Delete Button to Each Row in a PHPRunner Grid&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,4,8],"tags":[],"class_list":["post-1939","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-php-code-generator","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1939"}],"version-history":[{"count":6,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1939\/revisions"}],"predecessor-version":[{"id":3455,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1939\/revisions\/3455"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}