How to show a message yes/no before delete record?

Hi, i have create a small app for testing, in the grid radzen have create a button to delete the record, but i need to show a message yes/no before the delete operation.

I read the documentation and i read the post how to use the Condition to insert the confirm('delete this ?').

How to do this from blazor ?

Many Thanks

Aurelio

Same question here....

The Blazor DataGrid does not have built-in delete confirmation support yet. One can use the Blazor JSInterop though.

  1. Open the Page.razor.cs file and add the following code to the page class
         [Inject]
         IJSRuntime JSRuntime { get; set; }
    
         async Task<bool> Confirm(string message)
         {
              var result = await JSRuntime.InvokeAsync<bool>("confirm", new [] { message });
    
              return result;
         }
    
  2. Then in Radzen use the Invoke method action:
  3. Invoke the deleteXXX data source method if the Confirm method returned true.
1 Like

Hi..many tanks for the solution..

By Aurelio

Hello Radzen Team,
are you planning to add a "confirm delete" option on Blazor data grids? And on Button controls?
I use it on every delete action...

Regards
Thomas

We probably will at some point. Not scheduled at the moment though.

I use https://blazor.radzen.com/dialog for that.
Either you can have just a message saying "Are you sure?" or show what will be deleted.
It works very well.

1 Like

Where can i find the click event in datagrid?

Thanks for help
Thomas

I use visual studio so I am not familiar with the Radzen tool, unfortunately. I guess you could klick on the red icon with the X and the click event would be there?

No. I cant click on the red X. it must be an event on the grid?!

@Radzen Team: can you explain?

Regards
Thomas

Is a Button component in the DataGrid Column Template. You can use the template designer to reach that button:


With https://github.com/Basaingeal/Razor.SweetAlert2 can show a a message for confirm an action.

There is already simplified way to show dialogs including confirm:
https://blazor.radzen.com/dialog

Hello!

Doing this, every page with this dialog should then be excluded on Radzen Studio (not to be generated) ?

Thanks!

No, you can use the partial class of the page to define the method for showing simple dialog similar to the code in this thread:

Is there another overload for a function if someone selects the 'Yes' button?

Click=@(args => DialogService.Confirm("Are you sure?", "MyTitle", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" , YesButton=DoSomething() }))

this is fairly easy to do the steps are
1 - add your button
<RadzenButton Text="Delete confirmation" Click=@(args => DialogService.Confirm("Are you sure?", "All versions of this document will be deleted!", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" })) />

2 - In the OnInitialized subscribe

            DialogService.OnClose += CloseConfirmDelete;

3 - in the dispose unsubscribe

    public void Dispose()
    {
        // The DialogService is a singleton so it is advisable to unsubscribe.
        DialogService.OnClose -= CloseConfirmDelete;
    }

4 - add the function
void CloseConfirmDelete(dynamic result)
{
if(result != null) // if the user hits the x near the top right null is returned
{
// result is false if the user clicks no
if ((bool)result) DeleteDocument();
}
}

Warning - the function looks like it is getting called twice so you'll have to manage that as well.

1 Like

In this case, how do you know what document DeleteDocument has to delete? As by this point, you are already out of the context of the grid row.