---
title: "Confirm deletion with an alert dialog in C#"
url: https://recursiveai.org/oneapp/cookbook/confirm-destructive-action-with-alert-dialog/
description: "Confirm deletion in a C# desktop app with OneApp’s AlertDialog. Focus Cancel first, give the dialog an accessible name, and delete only on confirmation."
version: "1.1.1"
---

The only draft is “Buy tea.” Put keyboard focus on **Cancel** before the user can delete it.

```shell
dotnet new console -o Example
cd Example
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneApp.Kit
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```html
<AlertDialog InitialFocus="{Cancel}"
  Id="remove" Open="{Open}"
  OnOpenChange="{v => Open = v}">
  <template slot="trigger">
    <Button>Delete draft</Button>
  </template>
  <DialogTitle DialogId="remove">
    Delete the draft?
  </DialogTitle>
  <DialogDescription DialogId="remove">
    The text in this demo will be removed.
  </DialogDescription>
  <DialogFooter>
    <button ref="{Cancel}"
      on:click="KeepDraft">Cancel</button>
    <Button OnClick="{Delete}">
      Delete draft
    </Button>
  </DialogFooter>
</AlertDialog>
```

[The example](https://recursiveai.org/oneapp/cookbook/confirm-destructive-action-with-alert-dialog/confirm-destructive-action-with-alert-dialog.zip) is the full program below: unzip it and `dotnet run`.

The snippet goes in `Demo.html`; `Demo.cs` supplies the state, focus reference, and click handlers. Cancel uses a plain `<button>` so `ref` can point directly to the element for initial focus; Delete uses the kit’s `<Button>` because it only needs a click handler.

Click **Delete draft** to open the dialog, then choose **Delete draft** in the dialog to remove the sample text. The terminal prints:

```text
Draft deleted
```

## Full program

Checked on Linux (X11) with OneApp and OneApp Kit 1.1.1.

The zip pins both packages in `Example.csproj`. OneUI, OneApp’s UI layer, comes with `RecursiveAI.OneApp`; no separate install is needed. Its source generator implements the `[State]` properties in `Demo.cs`. It pairs `Demo.html` with the partial class `Demo` by matching the template’s base name to the class name.

```xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference
      Include="RecursiveAI.OneApp"
      Version="1.1.1" />
    <PackageReference
      Include="RecursiveAI.OneApp.Kit"
      Version="1.1.1" />
  </ItemGroup>
</Project>
```

`Demo.html`:

```html
<Theme>
  <main style="padding: 32px">
    <AlertDialog Id="remove" Open="{Open}"
      OnOpenChange="{v => Open = v}"
      InitialFocus="{Cancel}">
      <template slot="trigger">
        <Button Id="ask">
          Delete draft
        </Button>
      </template>
      <DialogTitle DialogId="remove">
        Delete the draft?
      </DialogTitle>
      <DialogDescription DialogId="remove">
        The text in this demo will be removed.
      </DialogDescription>
      <DialogFooter>
        <button id="cancel" ref="{Cancel}"
          on:click="KeepDraft">
          Cancel
        </button>
        <Button Id="confirm"
          OnClick="{Delete}">
          Delete draft
        </Button>
      </DialogFooter>
    </AlertDialog>
    <p id="draft">{Draft}</p>
  </main>
</Theme>
```

`Demo.cs`:

```csharp
using System;
using OneUI.Components;

public partial class Demo
{
    private ElementRef Cancel { get; } =
        new();

    [State]
    public partial bool Open { get; set; }
    [State]
    public partial string Draft { get; set; }

    public Demo() =>
        Draft = "Draft: Buy tea";

    private void KeepDraft() => Open = false;

    private void Delete()
    {
        Draft = "Draft deleted";
        Open = false;
        Console.WriteLine(Draft);
    }
}
```

`Program.cs` opens the window:

```csharp
using OneApp;

var options = new AppOptions(new Demo())
{
    Title = "Confirm delete",
    Width = 480,
    Height = 340,
};
using var host = AppHost.Create(options);
host.Run();
```

![Delete the draft dialog with Cancel focused and Delete draft beside it](https://recursiveai.org/oneapp/images/confirm-destructive-action-with-alert-dialog/dialog.png)

**Cancel** and Escape leave `Draft: Buy tea` in the window. Clicking the backdrop leaves the dialog open. If focus doesn’t land on Cancel in your app, check that `InitialFocus` and `ref` use the same `ElementRef` and that the target button is enabled.

## Variations

**Put Delete before Cancel.** If your layout calls for a different button order, keep `InitialFocus="{Cancel}"` and swap the buttons in `DialogFooter`:

```html
<Button Id="confirm" OnClick="{Delete}">
  Delete draft
</Button>
<button id="cancel" ref="{Cancel}"
  on:click="KeepDraft">Cancel</button>
```

## Related recipes

- [Show a confirmation dialog and a toast](https://recursiveai.org/oneapp/cookbook/show-dialog-and-toast-with-ui-kit/)
- [Automate UI tests for a C# desktop app](https://recursiveai.org/oneapp/cookbook/drive-desktop-app-from-tests/)

## The short answer

Use OneApp’s `AlertDialog` for a delete confirmation inside a C# desktop window. Give it a title and a description; bind `InitialFocus` to the Cancel button’s `ElementRef`. The trigger asks the question, and only the confirm button calls `Delete`.

## How do I confirm before deleting a record in C#?

Separate the user’s request to delete from the deletion itself. The button in the `trigger` slot just opens the prompt. `OnOpenChange` handles the dialog’s requests to update `Open`, while `KeepDraft` only closes the prompt. `Delete` changes the draft and closes the prompt. In the sample that change happens in memory: nothing on disk is deleted.

For a real list of records, name the record in the prompt, and have the confirmation act on the record that opened it, not on whatever item happens to be selected later. Use a verb like **Delete draft**, not **Yes**. If deletion fails, keep the record visible and show an error rather than clearing the list entry as if it succeeded.

## Where should keyboard focus go in a delete confirmation dialog?

Start on the action that does the least harm, usually **Cancel**. In this recipe `ref="{Cancel}"` binds the actual HTML button to an `ElementRef`. Passing that same reference to `InitialFocus` makes the choice independent of the button order. If the user opens the prompt and immediately presses Enter, the focused button should keep the draft, not delete it. The [WAI-ARIA modal dialog pattern](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/) recommends this approach for actions that are hard to undo.

An alert dialog is modal: Tab and Shift+Tab stay inside it, and Escape closes it without confirming. The kit wires the dialog’s accessible name and description from `DialogTitle` and `DialogDescription` when their `DialogId` matches the parent `Id`. The title asks what will happen; the description explains the consequence. See the [OneApp overlay reference](https://recursiveai.org/oneapp/docs/kit/#overlays) for the dialog’s keyboard behavior.
